{stringline;while(getline(cin,line)) //每次读入一整行,遇到空行直接跳过if(!line.empty()) cout<<line<<endl;return0; } size函数返回string对象的长度(即string对象中字符的个数) #include <iostream>#include<string>usingnamespacestd;intmain() {stringline;//每次读入一整行,输出其中超过100个字符的...
cout << str[i] << " "; cout << str.at(i) << " "; //字符修改 str[0] = 'x'; str.at(1) = 'x'; 2.1.8 string插入和删除 功能描述: 对string字符串进行插入和删除字符操作 函数原型: string& insert(int pos, const char* s); //插入字符串 string& insert(int pos, const strin...
c++ 为什么cout不能输出vector的内容< string>?你通过一个nullptr对象指针在一个无效的MyClass对象上调用...
//定义一个string类型的vector vector<string> s;//向容器内添加元素 s.push_back("abc");s.push_back("efg");s.push_back("hij");//输出容器内的元素到显示器上 cout << s[0] << endl;cout << s[1] << endl;cout << s[2] << endl;system("pause");return 0;} ...
string::iteratorpos=s.begin();inttimes;while(s.find(str,pos)!=string::npos){//当没有子串的时候返回npostimes++;cout<<pos<<endl;pos=s.find(str,pos)+1;//从后面位置继续匹配} 例子:获得小数字符串的整数部分和小数部分 stringa="123.456";//al、ar分别为第一个浮点数的整数部分、小数部分//截...
cout<<"V2"<< i <<endl; } 输出: 利用push_back添加元素,利用C++11的auto遍历vector对象,读取数据。 举例:利用cin读入一组整数并把他们存入一个vector对象V1 #include<iostream>#include<string>#include<vector>usingnamespacestd;usingstd::vector;intmain() { ...
cout<<str<<endl; 1. 2. 3. 用printf(“%s”,str);输出是会出问题的。这是因为“%s”要求后面的对象的首地址。但是string不是这样的一个类型,若一定要printf输出。那么可以加上.c_str()。 map map使用红黑树实现。查找时间在O(lg(n))-O(2*log(n))之间,构建map花费的时间比较长 ...
for(int i=0;i<nSize;i++){cout<<vecClass[i]<<" ";}cout<<endl; 需要注意的是:以方法一进行输出时,数组的下表必须保证是整数。 //打印vecClass ,方法二: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 for(int i=0;i<nSize;i++){cout<<vecClass.at(i)<<" ";}cout<<endl; ...
5、find()函数:在字符串中寻找子字符串,返回的是查找到的第一个子串的位置,未找到时返回string::npos(无符号整型) rfind():返回的是找到的最后一个子串的位置 unsigned int found=s.find(str); if(found!=string::npos) cout<<"the first position is"<<found<<endl; 6、substr(); //裁剪字符串,返回...
先来第一个string的比较 代码语言:javascript 代码运行次数:0 运行 AI代码解释 //用字符ascll码进行比较//=返回0//>返回1,<返回-1#include<string>#include<iostream>using namespace std;voidtest01(){string str1="hello";string str2="hello";if(str1.compare(str1)==0){cout<<"=";}elseif(str1...