STL container string basic usage summary, stl container string Summary
// Method: append (), insert (), erase (), empty (), replace (), // find (), compare (), empty () // algorithm: reverse () # include <iostream> # include <string> # include <vector> // string object as a vector element # include <sstream> // istringstream ostringstream # include <algorithm> // reverse () using namespace std; void showS (string s) {cout <s <endl;} template <typename T> string convertToString (T x) {// numeric value conversion to string object ostringstream o; o <x; Return o. str (); // Q: Why is 0.str() returned ()?} Template <typename T> T convertFromString (const string & s) {// The string object is converted to the istringstream I (s); T x; I> x; return x ;} int main () {string s; s + = "abc"; cout <s <endl; // output abc s. append ("123"); // append showS (s) to the end; // output abc123 cout <s [2] <endl; // subscript accesses s. insert (s. begin () + 1, 'M'); // insert showS (s); // output ambc123 cout <s. length () <endl; // length 7 s. erase (s. begin () + 2, s. begin () + 4); // Delete showS (s); // output am123 s = ""; // clear cout <s. empty () <endl; // whether it is null 1 // --------------------------------------------------- s = "abc123456"; showS (s); // output abc123456 s. replace (3, 3, "good"); // replace showS (s); // output abcgood456 cout <s. find ("cg") <endl // output 2 // search <s. find ("bb") <endl; // output 4294967295 cout <s. compare ("abcgo") <endl // compare, s large, 1 <s. compare ("abcgood456") <endl // equal, 0 <s. compare ("bbc") <endl; // s small,-1 reverse (s. begin (), s. begin () + 3); // use reverse to reverse sort showS (s); // output cbagood123456 string birth = convertToString (1997 ); // numeric value conversion to string object showS (birth); // output 1997 string dd = "7.18"; double p = convertFromString <double> (dd ); // string object to cout <p <endl; // output 7.18 // --------------------------------------------------------- vector <string> v; // string object as the vector element v. push_back ("Benz"); v. push_back ("Audi"); v. push_back ("BMW"); cout <v [0] <''<v [1] <'' <v [2] <endl; // output Benz Audi BMW cout <v [0] [0] <''<v [1] [1] <endl; // output B u cout <v [2]. length () <endl; // output 3 return 0 ;}