C++/String/string output

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Output a string with copy function

<source lang="cpp">

  1. include <algorithm>
  2. include <iostream>
  3. include <iterator>
  4. include <string>
  5. 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">

  1. include <iostream>
  2. 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">

  1. include <iostream>
  2. include <algorithm>
  3. 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>