string

Can someone explain the output line by line code for example what is append what is string::iterator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 string str = "Nobody is perfect";
string s = ""; // empty string
char *ch = "abcdef";
s.append(str,0,6);
cout << "s is : " << s << endl;
string::iterator inpIt1 = str.begin()+6;
string::iterator inpIt2 = str.end();
s.append(inpIt1,inpIt2);
cout << "s is : " << s << endl;
s.append(3,'!');
cout << "s is : " << s << endl;
s.append(ch,3);
cout << "s is : " << s << endl;
s.append(ch,3);
cout << "s is : " << s << endl;
Read this:

http://www.cplusplus.com/reference/string/string/

append is what it implies: it adds some characters to the string.

string::iterator is basically a pointer to a character in the string.
Just one thing why cant we write string inIt1=str.begin()+6;
in line 6
closed account (Dy7SLyTq)
you can do that. no one said you couldnt. its called pointer arithmatetic
But if we can do that why use iterator
closed account (Dy7SLyTq)
an iterator is just a pointer to a string. and you are'nt being clear. why based on the fact that you can do str.begin()+6 should you not have to use iterators?
The thing i am confused about if iterator is just a pointer than wouldn't it be correct just to have string *inpIt1=str.begin()+6;
is iterator just used to have genracity in program ?
than wouldn't it be correct just to have string *inpIt1=str.begin()+6;
No, it is not a pointer to string. It's a pointer to internal data. So it'd rather be char *.
On the other side: it's an iterator. All you need to know is that it obeys some according rules.
closed account (Dy7SLyTq)
No, it is not a pointer to string.
i apologize... my mistake. i havent built a container in a while. yeah its not actually a pointer to a string. its a pointer to what the class basic_string<char> contains.
THank you :)
Topic archived. No new replies allowed.