#include<iostream> #include<sstream> //使用istringstream必须包含的头文件 #include<string> #include<stdlib.h> #include<vector> using namespace std; //模板函数:将string类型变量转换为常用的数值类型 by maowei template <class Type> Type stringToNum(const string& str) { istringstream iss(str); Type num; iss>>num; return num; } int main(int argc,char *argv[]){ string com; vector<string> info(0); getline(cin,com); int start=0,end=0;//手工编写解读字符串信息的函数 while(start!=string::npos&&start!=com.size()){ start=com.find_first_not_of(' ',start); end=com.find_first_of(' ',start); if(end==string::npos){end=com.size();} info.push_back(com.substr(start,end-start)); start=end; } cout<<"info_size="<<info.size()<<endl; cout<<"info_capacitity="<<info.capacity()<<endl; for(int j=0;j<info.size();++j)cout<<info[j]<<endl; //test_template string s="123"; double x = stringToNum<double>(s); cout<<"x="<<x<<endl; vector<string> info_1; istringstream is(com);//采用istringstream从string对象str中读取字符 string s2; while(is>>s2){cout<<s2<<endl;info_1.push_back(s2);} cout<<"info_1_size="<<info_1.size()<<endl; cout<<"info_1_capacitity="<<info_1.capacity()<<endl; for(int j=0;j<info.size();++j)cout<<info_1[j]<<endl; return 0; }