C++/String/string output
Output a string with copy function
<source lang="cpp">
- include <algorithm>
- include <iostream>
- include <iterator>
- include <string>
- include <vector>
using namespace std; int main( ) {
string s( "this is a test." ); cout << s << endl; copy( s.begin(), s.end(), ostream_iterator<char>( cout, " " ) );
}
</source>
Read string in and then output
<source lang="cpp">
- include <iostream>
- include <string>
using namespace std; int main() {
string string1; string string2; cout << "\nEnter a string: "; getline( cin, string1 ); cout << "Enter a second string: "; getline( cin, string2 ); cout << "\nFirst string: " << string1 << endl; cout << "Second string: " << string2 << endl; cout << endl; return 0;
}
</source>
Use of as istream_iterator iterator
<source lang="cpp">
- include <iostream>
- include <algorithm>
- include <string>
using namespace std; int main() {
string words[5] = { "ABCD", "BCDEF", "CERF","DERT", "EFRE"}; string* where; where = find(words, words + 5, "CD"); cout << *++where << endl; sort(words, words + 5); where = find(words, words + 5, "ER"); cout << *++where << endl;
}
</source>