so im practicing stl string class, can not figure out why string->length function won't come correct answer of 5, , 2 (no matter actual length). here's program i'm trying run thinks there 2 items between ->begin , ->end:
void testfunc(string _string[]) { int _offset = 0; string::const_iterator i; (i = _string->begin(); != _string->end(); i++) { cout << _offset << "\t"; cout << _string[_offset] << endl; _offset ++; } }; int main() { string hello[] = {"hi", "holla", "eyo", "whatsup", "hello"}; testfunc(hello); char response; cin >> response; return 0; }
the output is:
0 hi 1 holla
thanks! =)
you're iterating through first string, "hi" - has 2 characters, see 2 entries.
if want go stl, you'd need vector instead of c-style array (i.e. vector<string>
, , use iterator on that.
if don't want stl:
void testfunc(string *strings, int stringcount) { int _offset = 0; while (stringcount--) { cout << _offset << "\t"; cout << _strings[_offset] << endl; _offset ++; } }; int main() { string hello[] = {"hi", "holla", "eyo", "whatsup", "hello"}; testfunc(hello, sizeof(hello) / sizeof(hello[0])); char response; cin >> response; return 0; }
Comments
Post a Comment