{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

c++-string - The string class • an instantiation of a...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
The string class an instantiation of a class template: typedef basic string<char> string; declared in the header file string for “old” C-style strings, use the header file cstring a string can contain any character; \ 0’ is not a special character >> and << can be used for input & output string s; // default ctor; empty string if (cin >> s) // read next word; skip leading whitespace cout << s; the getline function can also be used to input into a string string s; while (getline(cin, s)) { // for each line read (’\n’ is thrown away) } while (getline(cin, s, ’:’)) { // for each token read // tokens are separated by ’:’ (’:’ is thrown away) // token may contain ’\n’ } aw/07 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
several constructors string s1, // empty string s2(10, ’a’), // string of ten a’s s3("hello"), // contains ’h’,’e’,’l’,’l’,’o’ s4("hello!", 4),// ’h’,’e’,’l’,’l’ (1st 4 chars) s5(s3, 3), // ’l’,’o’ (start from pos 3) s6(s3, 1, 2), // ’e’,’l’ (2 chars, from pos 1) s7(s3); // copy of s3 Note : some string functions take arguments that specify a starting position & the number of chars (e.g. s6 above); these arguments have type string::size type which is an unsigned integral type For “number of chars”, the special value string::npos can usually be used to specify all remaining chars example: string s8(s3,3,string::npos) would give the same string as s5 above string::npos is defined as: static const size type npos = -1; /* ?? */ automatic conversion from const char *
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern