string arrays in c++
-
Consider the following: string mystring[] = {"Hello","World"}; How do I find how many elements my array of has? If for example I wanted to loop through the array and count the elements that had the letter 'L' or 'l' in them. displaying something like: element 1: 2 element 2: 1 My main focus here is how do I find out the number if elements in a given array. Thanks in advance for your help.
-
Consider the following: string mystring[] = {"Hello","World"}; How do I find how many elements my array of has? If for example I wanted to loop through the array and count the elements that had the letter 'L' or 'l' in them. displaying something like: element 1: 2 element 2: 1 My main focus here is how do I find out the number if elements in a given array. Thanks in advance for your help.
-
Consider the following: string mystring[] = {"Hello","World"}; How do I find how many elements my array of has? If for example I wanted to loop through the array and count the elements that had the letter 'L' or 'l' in them. displaying something like: element 1: 2 element 2: 1 My main focus here is how do I find out the number if elements in a given array. Thanks in advance for your help.
-
I know with regular arrays you would do a sizeof(mystring)/sizeof(string), but I really don't know about with string variable types. If it's broken, I probably did it bdiamond
Cool, What about if we take it one step further and have this: int main() { string mystring[] = {"ASD","asdf"}; int i; i = myfunc(mystring[]); cout << i; return 1; } int myfunc(string mystring[]) { int i; i = sizeof(mystring) / sizeof(mystring[0]; return i; } I get two different results when using the sizeof() in myfunc and sizeof in main.... Do I need to pass a pointer to the string??
-
Cool, What about if we take it one step further and have this: int main() { string mystring[] = {"ASD","asdf"}; int i; i = myfunc(mystring[]); cout << i; return 1; } int myfunc(string mystring[]) { int i; i = sizeof(mystring) / sizeof(mystring[0]; return i; } I get two different results when using the sizeof() in myfunc and sizeof in main.... Do I need to pass a pointer to the string??
You must pass the array size to myfunc. Myfunc receives a string pointer (that's all an array really is, a pointer to a contiguous block of strings), and has no way of knowing the size of the array that pointer points to. Inside the main function, the compiler substitutes the sizeof operator for the actual known size of your string array. However, inside the myfunc function, the compiler has no way to determine the size of the array passed. If you print the sizeof(mystring) in myfunc, you'll find it always equals 4, the size in bytes of a pointer (on the x86 architecture). It would probably be better if sizeof() never worked on arrays at all. At least then it would be consistent and less likely to confuse people who've never run into this before.
-
Consider the following: string mystring[] = {"Hello","World"}; How do I find how many elements my array of has? If for example I wanted to loop through the array and count the elements that had the letter 'L' or 'l' in them. displaying something like: element 1: 2 element 2: 1 My main focus here is how do I find out the number if elements in a given array. Thanks in advance for your help.
It might be preferable to find a way to use vector< string > mystring ; Then mystring.size () is obviously the size... Otherwise you could use a sentinel,
string mystring [] = { "Hello", "World", "" } ; void DoSomething ( string mystring []) { while ( !mystring->empty ()) { DoSomethingWithMystring ( mystring ) ; ++mystring ; } }
Paul