|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
vector<string> a;
string b;
cout << "输入字符串:";
while (getline(cin,b)) //问题出在这一行,如果换成getline(cin,b),初步满足要求,但是未去除空格;使用while时,程序没有响应
a.push_back(b);
for (auto it = a.begin(); it != a.end() && !it->empty(); it++){ //it为一个迭代器,遍历全部字符串,遇空串停止循环
for (auto it2 = it->begin(); it2 != it->end(); it2++) //利用迭代器遍历当前字符串
*it2 = toupper(*it2); //改写成大写形式
cout << *it << endl;
}
system("pause");
return 0;
}
- #include<iostream>
- #include<string>
- #include<vector>
- using namespace std;
- int main() {
- vector<string> a;
- string b;
- cout << "输入字符串:";
- getline(cin, b); //这儿直接getline
- ////////////////////////////////////////////////////////////////////////
- auto it = b.begin();
- while (it != b.end()) {
- if (*it <= 32) it = b.erase(it); //去掉所有不可打印字符和空格
- else it++;
- }
- /////////////////////////////////////////////////////////////////
- a.push_back(b);
- for (auto it = a.begin(); it != a.end() && !it->empty(); it++) {
- for (auto it2 = it->begin(); it2 != it->end(); it2++)
- *it2 = toupper(*it2);
- cout << *it << endl;
- }
- system("pause");
- return 0;
- }
复制代码
|
|