sizeof string array in for loop?
-
Hey, sorry if the threadtitle is a bit in-informative but I didn't know what to call it. Ok now to my problem, I am playing around with arrays and stuff see here ->
#include <iostream>
#include <string>using namespace std;
int main()
{
string a[] = { "one", "two", "three" };for (int i = 0;i <3;i++) { cout << a\[i\] << endl; } cin.get();
}
Now my problem is the "i < 3" let's say I wouldn't want to change that everytime i add another string to my array. How would I do that? Greetings!
-
Hey, sorry if the threadtitle is a bit in-informative but I didn't know what to call it. Ok now to my problem, I am playing around with arrays and stuff see here ->
#include <iostream>
#include <string>using namespace std;
int main()
{
string a[] = { "one", "two", "three" };for (int i = 0;i <3;i++) { cout << a\[i\] << endl; } cin.get();
}
Now my problem is the "i < 3" let's say I wouldn't want to change that everytime i add another string to my array. How would I do that? Greetings!
there is a ARRAYSIZE macro you can use : ARRAYSIZE(a). #define ARRAYSIZE(a) sizeof(a)/sizeof(a[0]) i don't recommend it. but it should work.
-
there is a ARRAYSIZE macro you can use : ARRAYSIZE(a). #define ARRAYSIZE(a) sizeof(a)/sizeof(a[0]) i don't recommend it. but it should work.
-
Hey, sorry if the threadtitle is a bit in-informative but I didn't know what to call it. Ok now to my problem, I am playing around with arrays and stuff see here ->
#include <iostream>
#include <string>using namespace std;
int main()
{
string a[] = { "one", "two", "three" };for (int i = 0;i <3;i++) { cout << a\[i\] << endl; } cin.get();
}
Now my problem is the "i < 3" let's say I wouldn't want to change that everytime i add another string to my array. How would I do that? Greetings!
If you are using Visual C++ you can use the macro
_countof(x)
which returns the size of the specified array. In your case you have to writei < _countof(a)
-
If you are using Visual C++ you can use the macro
_countof(x)
which returns the size of the specified array. In your case you have to writei < _countof(a)
-
You are welcome! :thumbsup:
-
Hey, sorry if the threadtitle is a bit in-informative but I didn't know what to call it. Ok now to my problem, I am playing around with arrays and stuff see here ->
#include <iostream>
#include <string>using namespace std;
int main()
{
string a[] = { "one", "two", "three" };for (int i = 0;i <3;i++) { cout << a\[i\] << endl; } cin.get();
}
Now my problem is the "i < 3" let's say I wouldn't want to change that everytime i add another string to my array. How would I do that? Greetings!
What's wrong with asking the compiler how big the array is - it knows, it bunged the thing on the stack for you:
template <typename T, std::size_t N>
std::size_t sizeof_array( T (&)[N] )
{
return N;
}Most decent compilers (VC++2003, 2005, 2008 and 2010, gcc 3.3 onwards) will inline that to a constant. You can extend it a bit...
template <typename T, std::size_t N>
T *beginning_of_array( T (&a)[N] )
{
return a;
}template <typename T, std::size_t N>
T *end_of_array( T (&a)[N] )
{
return &a[ N ];
}which means you can get away from using indexes entirely and start using algorithms to do the sort of thing you're doing in your example:
int main()
{
std::string a[] = { "One", "Two", "Three" };std::copy( beginning\_of\_array( a ), end\_of\_array( a ), std::ostream\_iterator<std::string>( std::cout, "\\n" ) );
}
From there you're not far away from using overloaded functions to come up with a generic way of handling different aggregates of things. Cheers, Ash