1:引言:
输入带空格的字符串 需要用到 getline(cin,str);具体在代码中看
2:上码
#include<bits/stdc++.h>
using namespace std;int main(){vector<string>v1;for( int i = 0; i < 2; i++ ){string temp;getline(cin,temp);//输入带空格的字符串v1.push_back(temp);}for( int i = 0; i < 2; i++ ){// cout << v1[i] << endl;string str = v1[i];
// cout << v1[i] << endl;for( int j = 0; j < str.size(); j++ ){if(str[j] == ' ')cout << "wyj" << endl; } }
}
如果没有用到vector容器的话 请看下面的码:
#include<bits/stdc++.h>
using namespace std;int main(){string str;getline(cin,str);for(int i = 0; i < str.size(); i++ ){if( str[i] == ' ')cout << "有空格" << endl;}}