M
Mike Wahler
kazack said:you'llWhich is better, or more efficient, or accepted?bebetter
off using a string variable instead of char. i.e.
#include <string>. You'll them be able to do what you seem to have in
mind.
string name;
name = "modem";
and you can still access the individual components of name:
for(int count = 0; count < strlen(name); count++)
cout << name[count] << "," ;
cout << endl;
outputs: m, o, d, e, m,
I thought the above code didn't look right. I have needed to do something
like this on more than one occasion and it just does not work.
strlen(name) gives an error of the following type:
cannot convert parameter 1 from 'class std ????' to const char *
What is the work around to this to do the same thing?
Use the interface, Luke!
name.size()
or
name.length()
Or if you insist upon using 'strlen()':
strlen(name.c_str())
-Mike
Forget about 'efficiency'. Strive to write correct, clear code.
Only if a profiler proves a performance problem should one be
concerned with optimzation.
Others might differ, but my opinion is that if you're using C++
and want to use a string, use std::string and its member
functions rather than 'C-style' strings.
-Mike