C++/String/string output

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

Output a string with copy function

  
#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, " " ) );
}


Read string in and then output

   
#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;
}


Use of as istream_iterator iterator

  
#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;              
}