STL container string basic usage summary, stl container string Summary

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.