C++
-
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?
-
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?
Here is a thread[^] that should answer your question, and then some.
Robust Services Core | Software Techniques for Lemmings | Articles
-
Here is a thread[^] that should answer your question, and then some.
Robust Services Core | Software Techniques for Lemmings | Articles
-
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?
-
Be aware that the standard
C++
library gently offers many alternative containers[^] to arrays.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
-
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
-
*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
-
*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
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_maptime ./lookup_linear_search
sum 252632422real 0m1,821s
user 0m1,811s
sys 0m0,008stime ./lookup_map
sum 252632422real 0m0,297s
user 0m0,297s
sys 0m0,000s -
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_maptime ./lookup_linear_search
sum 252632422real 0m1,821s
user 0m1,811s
sys 0m0,008stime ./lookup_map
sum 252632422real 0m0,297s
user 0m0,297s
sys 0m0,000sHmm, 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
-
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
-
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
: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