c++-string

C-string - The string class • an instantiation of a class template typedef basic string string • declared in the header file string •

Info iconThis 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 << 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
Background image of page 1

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

View Full DocumentRight 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 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 * into strings, but no
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/05/2009 for the course COMM 1100 taught by Professor N during the Spring '09 term at BC.

Page1 / 7

C-string - The string class • an instantiation of a class template typedef basic string string • declared in the header file string •

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online