Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++

C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Member_14611814

    I am trying to learn c++ programming language and am still an amateur at it. I have a problem with "passing arrays in a function" and all am asking is what would be the easiest way to handle this problem?

    Greg UtasG Offline
    Greg UtasG Offline
    Greg Utas
    wrote on last edited by
    #2

    Here is a thread[^] that should answer your question, and then some.

    Robust Services Core | Software Techniques for Lemmings | Articles

    <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
    <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

    L 1 Reply Last reply
    0
    • Greg UtasG Greg Utas

      Here is a thread[^] that should answer your question, and then some.

      Robust Services Core | Software Techniques for Lemmings | Articles

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      Greg Utas wrote:

      and then some

      Should that be "and then lots"? ;)

      1 Reply Last reply
      0
      • M Member_14611814

        I am trying to learn c++ programming language and am still an amateur at it. I have a problem with "passing arrays in a function" and all am asking is what would be the easiest way to handle this problem?

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #4

        Be aware that the standard C++ library gently offers many alternative containers[^] to arrays.

        In testa che avete, signor di Ceprano?

        C 1 Reply Last reply
        0
        • CPalliniC CPallini

          Be aware that the standard C++ library gently offers many alternative containers[^] to arrays.

          C Offline
          C Offline
          charlieg
          wrote on last edited by
          #5

          and are extremely useful, though sometimes performance is mystifying :)

          Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759

          CPalliniC 1 Reply Last reply
          0
          • C charlieg

            and are extremely useful, though sometimes performance is mystifying :)

            Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #6

            The right container for the right job...

            In testa che avete, signor di Ceprano?

            C 1 Reply Last reply
            0
            • CPalliniC CPallini

              The right container for the right job...

              C Offline
              C Offline
              charlieg
              wrote on last edited by
              #7

              *sometimes*. And it may very well depend on the platform. I have a communications system that transmits data using tags and a parser. I have upwards of 500 data elements that can go back and forth. The initial implementation used a simple linear search for finding a passed tag. As time passed, the tag set became larger and larger. Hey, this is a good place for a mapped data set, allowing me to find the tag quickly I thought. The map turned out to be 3 times slower than the linear search. Never did figure out why. Showed the sample code to someone who loves the STL and has a lot more experience than I do. No idea. Someday I'll get back to it.

              Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759

              CPalliniC 1 Reply Last reply
              0
              • C charlieg

                *sometimes*. And it may very well depend on the platform. I have a communications system that transmits data using tags and a parser. I have upwards of 500 data elements that can go back and forth. The initial implementation used a simple linear search for finding a passed tag. As time passed, the tag set became larger and larger. Hey, this is a good place for a mapped data set, allowing me to find the tag quickly I thought. The map turned out to be 3 times slower than the linear search. Never did figure out why. Showed the sample code to someone who loves the STL and has a lot more experience than I do. No idea. Someday I'll get back to it.

                Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #8

                My little experiment gives me opposite results:

                #include #include using namespace std;

                constexpr int Indices = 4096;
                constexpr int Size = 512;

                extern const int idx[Indices];
                extern const string tag [Size];

                int linear_search(const string & s )
                {
                for (int n = 0; n mtag;
                for (int n=0; nsecond;
                #endif
                sum+=k;
                }

                cout << "sum " << sum << endl;
                }

                Where

                • tag is an array of 512 randomly generated strings (having length betwween 4 and 12)
                • idx is an array of 4096 randomly generated indices (for quickly gatering a candidate)

                Output

                g++ -D LINEAR_SEARCH -Wall lookup.cpp -o lookup_linear_search
                g++ -Wall lookup.cpp -o lookup_map

                time ./lookup_linear_search
                sum 252632422

                real 0m1,821s
                user 0m1,811s
                sys 0m0,008s

                time ./lookup_map
                sum 252632422

                real 0m0,297s
                user 0m0,297s
                sys 0m0,000s

                In testa che avete, signor di Ceprano?

                C 1 Reply Last reply
                0
                • CPalliniC CPallini

                  My little experiment gives me opposite results:

                  #include #include using namespace std;

                  constexpr int Indices = 4096;
                  constexpr int Size = 512;

                  extern const int idx[Indices];
                  extern const string tag [Size];

                  int linear_search(const string & s )
                  {
                  for (int n = 0; n mtag;
                  for (int n=0; nsecond;
                  #endif
                  sum+=k;
                  }

                  cout << "sum " << sum << endl;
                  }

                  Where

                  • tag is an array of 512 randomly generated strings (having length betwween 4 and 12)
                  • idx is an array of 4096 randomly generated indices (for quickly gatering a candidate)

                  Output

                  g++ -D LINEAR_SEARCH -Wall lookup.cpp -o lookup_linear_search
                  g++ -Wall lookup.cpp -o lookup_map

                  time ./lookup_linear_search
                  sum 252632422

                  real 0m1,821s
                  user 0m1,811s
                  sys 0m0,008s

                  time ./lookup_map
                  sum 252632422

                  real 0m0,297s
                  user 0m0,297s
                  sys 0m0,000s

                  C Offline
                  C Offline
                  charlieg
                  wrote on last edited by
                  #9

                  Hmm, your code is surprising close to mine. What compiler are you using? I have to use VS2008 for embedded work, so maybe the STL implementation is suspect. I'll have to dust it off. Your results are what I would have expected.

                  Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759

                  CPalliniC 1 Reply Last reply
                  0
                  • C charlieg

                    Hmm, your code is surprising close to mine. What compiler are you using? I have to use VS2008 for embedded work, so maybe the STL implementation is suspect. I'll have to dust it off. Your results are what I would have expected.

                    Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #10

                    Quote:

                    What compiler are you using?

                    The good one. :laugh: The hint is in the command line[^]:

                    g++ -D LINEAR_SEARCH -Wall lookup.cpp -o lookup_linear_search

                    In testa che avete, signor di Ceprano?

                    C 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      Quote:

                      What compiler are you using?

                      The good one. :laugh: The hint is in the command line[^]:

                      g++ -D LINEAR_SEARCH -Wall lookup.cpp -o lookup_linear_search

                      C Offline
                      C Offline
                      charlieg
                      wrote on last edited by
                      #11

                      :laugh:

                      Charlie Gilley <italic>Stuck in a dysfunctional matrix from which I must escape... "Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups