始)替换本字符串中的字符,从index1开始,最多num1个字符? 3.用str中的num个字符(从index开始)替换本字符串中的字符? 4.用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符? 5.用num2个ch字符替换本字符串中的字符,从index开始? 6.用str中的字符替换本字符串中的字符,迭代器start和end指示范围? 7.用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围,? 8.用num个ch字符替换本字符串中的内容,迭代器start和end指示范围.? */? void test4()? {???? ??? string s = "They say he carved it himself...from a BIGGER spoon";? ??? string s2 = "find your Homer.";? ??? s.replace( 32, s2.length(), s2 );? ??? cout << s << endl;?? //They say he carved it himself...find your Homer.oon?? }??? //其他?? /* 交换(swap)? void swap( basic_string &str );//把str和本字符串交换 ? basic_string substr( size_type index, size_type num = npos ); substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。 ? size_type size();//size()函数返回字符串中现在拥有的字符数。 size_type length();//length()函数返回字符串的长度. 这个数字应该和size()返回的数字相同. ? void clear();//清空 bool empty();//如果字符串为空则empty()返回真(true),否则返回假(false). ? const char *data();//data()函数返回指向自己的第一个字符的指针. const char *c_str();//c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同.? */? void test5()? {???? ??? string first( "This comes first" );? ??? string second( "And this is second" );? ??? first.swap( second );? ??? cout << first << endl;//And this is second?? ??? cout << second << endl;//This comes first?? ? ??? string s("What we have here is a failure to communicate");? ??? string sub = s.substr(5);?? ??? cout << sub << endl;//we have here is a failure to communicate?? ? ??? cout<<"sub size:"< ????????? ??? sub.clear();//<==>sub="";?? ??? cout< }?? //C++11?? //转换成 int;float;double;long;long long;long double;unsigned long;unsigned long long;?? //stoi(),stof(),stod(),stold(),stol(),stoll(),stoul(),stoull(),?? void test6()? {????? ??? const char *str1 = " 42";? ??? const char *str2 = "3.14159";? ??? const char *str3 = "31337 with words";?? ??? std::cout << "std::stoi(\"" << str1 << "\") is " << std::stoi(str1) << '\n';//42?? ??? std::cout << "std::stoi(\"" << str2 << "\") is " << std::stoi(str2) << '\n';//3?? ??? std::cout << "std::stoi(\"" << str3 << "\") is " << std::stoi(str3) << '\n';//31337??? ? ??? std::cout << "std::stof(\"" << str1 << "\") is " << std::stof(str1,NULL) << '\n';//42?? ??? std::cout << "std::stof(\"" << str2 << "\") is " << std::stof(str2) << '\n';//3.14159?? ??? std::cout << "std::stof(\"" << str3 << "\") is " << std::stof(str3) << '\n';//31337??? ? ??? std::cout << "std::stod(\"" << str1 << "\") is " << std::stod(str1) << '\n';//42?? ??? std::cout << "std::stod(\"" << str2 << "\") is " << std::stod(str2,NULL) << '\n';//3.14159?? ??? std::cout << "std::stod(\"" << str3 << "\") is " << std::stod(str3) << '\n';//31337??? ????? ??? std::cout << "std::stold(\"" << str1 << "\") is " << std::stold(str1) << '\n';//42?? ??? std::cout << "std::stold(\"" << str2 << "\") is " << std::stold(str2) << '\n';//3.14159?? ??? std::cout << "std::stold(\"" << str3 << "\") is " << std::stold(str3,NULL) << '\n';//31337??? ????? ??? string s1 = "-8246821";//十进制(默认)??? ??? string s2 = "-0xffff"; //十六进制?? ??? string s3 = "-020";? //八进制?? ??? string s4 = "11111110";//是十进制,若要二进制则:stol(s4,NULL,2)?? ??? //long? 就是 int的加长版,而不是float的加长版?? ??? cout<<"stol("< ??? cout<<"stol("< ??? cout<<"stol("< ??? cout<<"stol("< ? ??? cout<<"stoul("< ??? cout<<"stoul("< ??? cout<<"stoul("< ??? cout<<"stoul("< ? ??? cout<<"stoll("< ??? cout<<"stoll("< ??? cout<<"stoll("< ??? cout<<"stoll("< ? ??? cout<<"stoull("< ??? cout<<"stoull("< ??? cout<<"stoull("< ??? cout<<"stoull("< }?? //C++11?? //num to string/wstring :to_string(),to_wstring()?? void test7()? {? ??? long double f=3.1415926;? ??? std::wstring wpi = L"pi is " + std::to_wstring(f);? ??? wstring wperfect = std::to_wstring(_ULonglong(1+2+4+7+14)) + L" is a perfect number";? ??? wcout << wpi << L'\n';//3.14159?? ??? wcout << wperfect << L'\n';//28?? ? ??? std::string pi = "pi is " + std::to_string(f);? ??? string perfect = std::to_string(long long(1+2+4+7+14)) + " is a perfect number";? ??? cout << pi << endl;//3.14159?? ??? cout << perfect << endl;//28?? }? void Test(char h)? {? ??? cout<<"press key===="< ??? switch(h)? ??? {?? ??? case '0':? test0();break;? ??? case '1':? test1();break;? ??? case '2':? test2();break;? ??? case '3':? test3();break;? ??? case '4':? test4();break;? ??? case '5':? test5();break;?? ??? case '6':? test6();break;?? ??? case '7':? test7();break;?? ??? case 27:? ??? case 'q':exit(0);break;?? ??? default:cout<<"default "< ??? }? }? void main()? {? ??? while(1)? ??? {? ??????? Test(getch());? ??? }?? }??
#include #include #include using namespace std; //1.构造函数 /* string(); string( size_type length, char ch ); string( const char *str ); string( const char *str, size_type index ); string( string &str, size_type index, size_type length ); string( input_iterator start, input_iterator end ); 字符串的构造函数创建一个新字符串,包括: 1.以length为长度的ch的拷贝(即length个ch) 2.以str为初值 (长度任意), 3.以index为索引开始的子串,长度为length,或者 4.以从start |