Note09--String

书:C++大学教程(第七版)
1. getline(cin, string1);
从键盘上读取一个字符串到string1.输入用一个换行符结束,所以getline函数可以读取一行文本到一个string类对象中。

#include <iostream>
#include <string>
using namespace std;
int main()
{
   string string1( "cat" );
   string string2; // initialized to the empty string
   string string3; // initialized to the empty string
   string2 = string1; // assign string1 to string2
   string3.assign( string1 ); // assign string1 to string3
   cout << "string1: " << string1 << "\nstring2: " << string2
      << "\nstring3: " << string3 << "\n\n";
   // modify string2 and string3 
   string2[ 0 ] = string3[ 2 ] = 'r';
   cout << "After modification of string2 and string3:\n" << "string1: " 
      << string1 << "\nstring2: " << string2 << "\nstring3: ";
   // demonstrating member function at
   for ( int i = 0; i < string3.length(); i++ ) 
      cout << string3.at( i );
   // declare string4 and string5
   string string4( string1 + "apult" ); // concatenation
   string string5;
   // overloaded +=                               
   string3 += "pet"; // create "carpet"           
   string1.append( "acomb" ); // create "catacomb"
   // append subscript locations 4 through end of string1 to
   // create string "comb" (string5 was initially empty)
   string5.append( string1, 4, string1.length() - 4 );

   cout << "\n\nAfter concatenation:\nstring1: " << string1 
      << "\nstring2: " << string2 << "\nstring3: " << string3 
      << "\nstring4: " << string4 << "\nstring5: " << string5 << endl;
}  // end main
  1. 字符串比较compare
string string1( "Testing the comparison functions." );
   string string2( "Hello" ); 
   string string3( "stinger" );
   string string4( string2 );

   cout << "string1: " << string1 << "\nstring2: " << string2
      << "\nstring3: " << string3 << "\nstring4: " << string4 << "\n\n";
   // comparing string1 and string4
   if ( string1 == string4 )
      cout << "string1 == string4\n";
   else // string1 != string4 
   { 
      if ( string1 > string4 )
         cout << "string1 > string4\n";
      else // string1 < string4
         cout << "string1 < string4\n";
   } // end else
   // comparing string1 and string2
   int result = string1.compare( string2 );
   if ( result == 0 )
      cout << "string1.compare( string2 ) == 0\n";
   else // result != 0
   {
      if ( result > 0 )
         cout << "string1.compare( string2 ) > 0\n";
      else // result < 0
         cout << "string1.compare( string2 ) < 0\n";
   } // end else
   // comparing string1 (elements 2-5) and string3 (elements 0-5)
   result = string1.compare( 2, 5, string3, 0, 5 );
   if ( result == 0 )
      cout << "string1.compare( 2, 5, string3, 0, 5 ) == 0\n";
   else // result != 0
   {
      if ( result > 0 )
         cout << "string1.compare( 2, 5, string3, 0, 5 ) > 0\n";
      else // result < 0
         cout << "string1.compare( 2, 5, string3, 0, 5 ) < 0\n";
   } // end else
   // comparing string2 and string4
   result = string4.compare( 0, string2.length(), string2 );
   if ( result == 0 )
      cout << "string4.compare( 0, string2.length(), " 
         << "string2 ) == 0" << endl;
   else // result != 0
   {
      if ( result > 0 )
         cout << "string4.compare( 0, string2.length(), "
            << "string2 ) > 0" << endl;
      else // result < 0
         cout << "string4.compare( 0, string2.length(), "
            << "string2 ) < 0" << endl;
   } // end else

   // comparing string2 and string4
   result = string2.compare( 0, 3, string4 );
   if ( result == 0 )
      cout << "string2.compare( 0, 3, string4 ) == 0" << endl;
   else // result != 0
   {
      if ( result > 0 )
         cout << "string2.compare( 0, 3, string4 ) > 0" << endl;
      else // result < 0
         cout << "string2.compare( 0, 3, string4 ) < 0" << endl;
   } // end else
  1. 子字符串substr: string1.substr(7, 5)
  2. 交换字符串:first.swap(second)
  3. 查找字符串中的字串和字符
string string1( "noon is 12 pm; midnight is not." );
   int location;

   // find "is" at location 5 and 24
   cout << "Original string:\n" << string1 
      << "\n\n(find) \"is\" was found at: " << string1.find( "is" ) 
      << "\n(rfind) \"is\" was found at: " << string1.rfind( "is" );
   // find 'o' at location 1
   location = string1.find_first_of( "misop" );
   cout << "\n\n(find_first_of) found '" << string1[ location ]
      << "' from the group \"misop\" at: " << location;

   // find 'o' at location 29
   location = string1.find_last_of( "misop" );
   cout << "\n\n(find_last_of) found '" << string1[ location ] 
        << "' from the group \"misop\" at: " << location;

   // find '1' at location 8 
   location = string1.find_first_not_of( "noi spm" );
   cout << "\n\n(find_first_not_of) '" << string1[ location ]
      << "' is not contained in \"noi spm\" and was found at: " 
      << location;

   // find '.' at location 12
   location = string1.find_first_not_of( "12noi spm" );
   cout << "\n\n(find_first_not_of) '" << string1[ location ]
      << "' is not contained in \"12noi spm\" and was " 
      << "found at: " << location << endl;
   // search for characters not in string1
   location = string1.find_first_not_of( 
      "noon is 12 pm; midnight is not." );
   cout << "\nfind_first_not_of(\"noon is 12 pm; midnight is not.\")"  
      << " returned: " << location << endl;
  1. 在字符串中替换字符
string string1( "The values in any left subtree"
      "\nare less than the value in the"
      "\nparent node and the values in"
      "\nany right subtree are greater"
      "\nthan the value in the parent node" );

   cout << "Original string:\n" << string1 << endl << endl;
   // remove all characters from (and including) location 62 
   // through the end of string1
   string1.erase( 62 );
   // output new string
   cout << "Original string after erase:\n" << string1
      << "\n\nAfter first replacement:\n";

   int position = string1.find( " " ); // find first space
   // replace all spaces with period
   while ( position != string::npos ) 
   {
      string1.replace( position, 1, "." );
      position = string1.find( " ", position + 1 );
   } // end while

   cout << string1 << "\n\nAfter second replacement:\n";

   position = string1.find( "." ); // find first period
   // replace all periods with two semicolons
   // NOTE: this will overwrite characters
   while ( position != string::npos )
   {
      //用"xxxxx;;yyy"中从第五个位置开始连续两个字符替换string1中从position开始连续两个字符
      string1.replace( position, 2, "xxxxx;;yyy", 5, 2 );
      position = string1.find( ".", position + 1 );
   } // end while
   cout << string1 << endl;
  1. 插入字符
string string1( "beginning end" );
   string string2( "middle " );
   string string3( "12345678" );
   string string4( "xx" );
   cout << "Initial strings:\nstring1: " << string1
      << "\nstring2: " << string2 << "\nstring3: " << string3
      << "\nstring4: " << string4 << "\n\n";
   // insert "middle" at location 10 in string1
   string1.insert( 10, string2 );
   // insert "xx" at location 3 in string3
   string3.insert( 3, string4, 0, string::npos );
   cout << "Strings after insert:\nstring1: " << string1
      << "\nstring2: " << string2 << "\nstring3: " << string3
      << "\nstring4: " << string4 << endl;
  1. 转换成C风格的char *字符串
string string1( "STRINGS" ); // string constructor with char* arg
   const char *ptr1 = 0; // initialize *ptr1
   int length = string1.length();
   char *ptr2 = new char[ length + 1 ]; // including null

   // copy characters from string1 into allocated memory
   string1.copy( ptr2, length, 0 ); // copy string1 to ptr2 char*
   ptr2[ length ] = '\0'; // add null terminator
   cout << "string string1 is " << string1
      << "\nstring1 converted to a C-Style string is "
      << string1.c_str() << "\nptr1 is ";
   // Assign to pointer ptr1 the const char * returned by
   // function data(). NOTE: this is a potentially dangerous
   // assignment. If string1 is modified, pointer ptr1 can
   // become invalid.
   ptr1 = string1.data();  

   // output each character using pointer
   for ( int i = 0; i < length; i++ )
      cout << *( ptr1 + i ); // use pointer arithmetic
   cout << "\nptr2 is " << ptr2 << endl;
   delete [] ptr2; // reclaim dynamically allocated memory
  1. 迭代器
string string1( "Testing iterators" );
   string::const_iterator iterator1 = string1.begin();
   cout << "string1 = " << string1
      << "\n(Using iterator iterator1) string1 is: ";
   // iterate through string
   while ( iterator1 != string1.end() ) 
   {
      cout << *iterator1; // dereference iterator to get char
      iterator1++; // advance iterator to next char
   } // end while
   cout << endl;
  1. istringstream和ostringstream
    类istringstream支持从字符串输入,类ostringstream支持输出到一个字符串。
    类名istringstream和ostringstream实际上是由typedef定义的别名:
 typedef basic_istringstream< char > istringstream;
    typedef basic_ostringstream< char > ostringstream;
   ostringstream outputString; // create ostringstream instance
   string string1( "Output of several data types " );
   string string2( "to an ostringstream object:" );
   string string3( "\n        double: " );
   string string4( "\n           int: " );
   string string5( "\naddress of int: " );
   double double1 = 123.4567;
   int integer = 22;
   // output strings, double and int to ostringstream outputString         
   outputString << string1 << string2 << string3 << double1  
      << string4 << integer << string5 << &integer;
   // call str to obtain string contents of the ostringstream
   cout << "outputString contains:\n" << outputString.str();
   // add additional characters and call str to output string
   outputString << "\nmore characters added";
   cout << "\n\nafter additional stream insertions,\n"
      << "outputString contains:\n" << outputString.str() << endl;
   string input( "Input test 123 4.7 A" );
   istringstream inputString( input );
   string string1;
   string string2;
   int integer;
   double double1;
   char character;
   inputString >> string1 >> string2 >> integer >> double1 >> character;
   cout << "The following items were extracted\n"
      << "from the istringstream object:" << "\nstring: " << string1
      << "\nstring: " << string2 << "\n   int: " << integer
      << "\ndouble: " << double1 << "\n  char: " << character;
   // attempt to read from empty stream
   long value;
   inputString >> value;
   // test stream results
   if ( inputString.good() )
      cout << "\n\nlong value is: " << value << endl;
   else
      cout << "\n\ninputString is empty" << endl;
  1. 字符串的赋值和连接
    (1) 提供了重载的运算符=和成员函数assign来对字符串进行复制;
    (2) 下标运算符[]提供对一个字符串的任何元素的读/写访问;
    (3) 成员函数at提供溢出检查,超出字符串的任意边界都将抛出out_of_range异常。下标运算符[]不提供溢出检查;
    (4) 类提供重载的+和+=运算符以及成员函数append来执行字符串的连接操作;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值