C/C++:string和stringstream的小例子

2015-01-27 10:05:39 · 作者: · 浏览: 9



C++字符串常用操作

1、string的可以用相加来连接两个字符串。



#include
  
   
#include
   
     using namespace std; int main() { string str; str += "hello "; str += "world !"; cout << str << endl; return 0; } 
   
  

2、stringstream的运用:



#include
  
   
#include
   
     #include
    
      using namespace std; int main() { stringstream ss; ss << "hello"; //stringstream重载了“<<”运算符,可直接这么利用 ss << " "; ss << "world ! "; ss << 2014; cout << ss.str() << endl; return 0; }