C++/String/string

Материал из C\C++ эксперт
Версия от 10:25, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A short string demonstration.

  
#include <iostream>
#include <string>
using namespace std;
   
int main()
{
  string str1("A");
  string str2("B");
  string str3("O");
  string str4;
   
  str4 = str1;
  cout << str1 << "\n" << str3 << "\n";
   
  str4 = str1 + str2;
  cout << str4 << "\n";
   
  str4 = str1 + " to " + str3;
  cout << str4 << "\n";
   
  if(str3 > str1) 
     cout << "str3 > str1\n";
  if(str3 == str1+str2)
    cout << "str3 == str1+str2\n";
   
  str1 = "This is a null-terminated string.\n";
  cout << str1;
   
  string str5(str1);
  cout << str5;
   
  cout << "Enter a string: ";
  cin >> str5;
  cout << str5;
   
  return 0;
}


Changing Case in Strings

  
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
inline
char my_tolower( char c )
{  return
   static_cast<char>( tolower( static_cast<unsigned char>( c ) ) );
}
inline
char my_toupper( char c )
{  return
   static_cast<char>( toupper( static_cast<unsigned char>( c ) ) );
}
int main( )
{
   string book( "The C++ Programming Language, 3rd Edition" );
   cout << "String:" << book << endl << endl;
   transform( book.begin(), book.end(), book.begin(), my_toupper );
   cout << "Big letters:" << book << endl << endl;
   transform( book.begin(), book.end(), book.begin(), my_tolower );
   cout << "Small letters:" << book;
}


Char Escapes

  
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string y1( "a\x62" );
    cout << y1 << endl;   // y1 is string "ab".
                                                      
    string y6( "a\xef" );
    cout << y6 << endl;                               
                                                      
    string w1( "a\142" );
    cout << w1 << endl;                               
                                                      
    string w2( "a\142c" );
    cout << w2 << endl;                               
                                                      
    string w3( "a\142142" );
    cout << w3 << endl;                               
                                                      
    string w4( "a\79" );
    cout << w4 << endl;                               
                                                      
    string w5( "\x00007p\x0007q\x0007r\x007s\x07t\x7u" );
    
    cout << w5 << endl; 
    return 0;
}


copy constructor

  
 
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
   string s1( "AA1234567890asdfasdf" );
   string s2( " AAB" );
   string s3;
   // test copy constructor
   string *s4Ptr = new string( s1 );  
   cout << "\n*s4Ptr = " << *s4Ptr << "\n\n";
   return 0;
}
 /* 
*s4Ptr = AA1234567890asdfasdf

 */


Define a string variable, assign a value and display it

  
 
#include <iostream>
#include <string>
using namespace std ;
using std::cout;       
using std::cin;
int main()
{
  string s = "www.java2s.com \n";
  cout <<s; 
  return 0;
}
/* 
www.java2s.com
 */


Extracting Words in Strings Delimited by Whitespace

  
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector> 
using namespace std;

int main( )
{
   string text( "this is a test" ); 
   istringstream stream( text ); 
   // make a vector with each word of the text
   vector< string > words( (istream_iterator<string>( stream )),istream_iterator<string>() ); 
   cout << words.size() << " : " << text;
}


Loop through the string array

  
 
#include <iostream>
#include <list>
#include <algorithm>
#include <string>
using namespace std;
int main( ) {
   string arrStr[5] = {"A", "B", "C", "D", "E"};
   for (string* p = &arrStr[0]; p != &arrStr[5]; ++p) {
      cout << *p << endl;
   }
}
/* 
A
B
C
D
E
 */


string basics

  
 

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string str1("Alpha");
  string str2("Beta");
  string str3("Omega");
  string str4;
  // assign a string
  str4 = str1; 
  cout << str1 << "\n" << str3 << "\n";
  // concatenate two strings
  str4 = str1 + str2; 
  cout << str4 << "\n";
  // concatenate a string with a C-string
  str4 = str1 + " to " + str3;
  cout << str4 << "\n";
  // compare strings
  if(str3 > str1) cout << "str3 > str1\n";
  if(str3 == str1+str2)
    cout << "str3 == str1+str2\n";
  /* A string object can also be 
     assigned a normal string. */
  str1 = "This is a null-terminated string.\n";
  cout << str1;
  // create a string object using another string object
  string str5(str1);
  cout << str5;
  // input a string
  cout << "Enter a string: ";
  cin >> str5;
  cout << str5;
  return 0;
}
/* 
Alpha
Omega
AlphaBeta
Alpha to Omega
str3 > str1
This is a null-terminated string.
This is a null-terminated string.
Enter a string: a string
a
 */