vector<string> vec_str;
string str;
while(cin>>str)
vec_str.push_back(str);
string a;
getline(cin,a)
输入有两行,第一行n
第二行是n个空格隔开的字符串
int n;
cin>>n;
vector<string> vec_str;
string str;
while(n--)
{
cin>>str;
vec_str.push_back(str);
str.clear();
}
int n;
int m;
vector<vector<string>> vec_strs;
cin>>n;
cin>>m;
while(n--)
{
vector<string> vec_str;
while(m--)
{
string str;
cin>>str;
vec_str.push_back(str);
}
vec_strs.push_back(vec_str);
}
int n;
vector<string> vec_strs;
cin>>n;
while(n--)
{
string> vec_str;
get_line(cin,vec_str);
vec_strs.push_back(vec_str);
}
1 2 3
//方法1:getchar
//代码通过cin.get()从缓存中读取一个字节,这样就扩充了cin只能用空格和TAB两个作为分隔符。
//这很精巧。发现是’\n’就知道一行结束了
vector<int> nums;
int num;
while(cin>>num){
nums.push_back(num);
if(getchar() == '\n')
break;
}
//方法2:cin.get
vector<int> nums;
int num;
while(cin>>num){
nums.push_back(num);
if(cin.get() == '\n')
break;
}
3
1,2,3
int m;
cin >> m;
char sep;
vector<int> nums(m);
for (int i = 0; i < m - 1; ++i){
cin >> nums[i] >> sep;
}
cin >> nums[m - 1];
#include
#include
#include
#include
#include
using namespace std;
int main() {
string input;
while (getline(cin, input)) { //读取一行
vector<string> strs;
string str;
stringstream ss(input);
while (getline(ss, str, ',')) {
strs.push_back(str);
}
sort(strs.begin(), strs.end());
for (auto& str : strs) {
cout << str << " ";
}
cout << endl;
}
return 0;
}
hello,world,feng,haojun
feng haojun hello world
int main() {
string input;
while (getline(cin, input)) { //读取一行
stringstream data(input); //使用字符串流
int num = 0, sum = 0;
while (data >> num) {
sum += num;
}
cout << sum << endl;
}
return 0;
}
1 2 3
6
输入用逗号隔开的字符串,去除字符串填入数组
#include
#include
#include
#include
#include
using namespace std;
int main()
{
int n;
cin >> n;
vector<vector<int>> vec(n, vector<int>(n));
for (int i = 0; i < n; ++i) {//注意这里一定要有i控制,用while容易一直输入导致错误
string s;
cin >> s;
replace(s.begin(), s.end(), ',', ' ');
istringstream input(s);
string tmp;
for (int j = 0; j < n; ++j) {//内层循环也很重要
input >> tmp;
vec[i][j] = stoi(tmp);
}
}
return 0;
}
3
1,2,3,3,2
4,5,6,1,3
7,8,9,3,4
using namespace std;
int main()
{
int n;
cin >> n;
vector<vector<int>> vec(n,vector<int>(5));
for (int i = 0; i < n;++i){//注意这里一定要有i控制,用while容易一直输入导致错误
string s;
cin >> s;
replace(s.begin(), s.end(), ',', ' ');
istringstream input(s);
string tmp;
for (int j = 0; j < 5;++j){//内层循环也很重要
input >> tmp;
vec[i][j] = stoi(tmp);
}
}
for (int i = 0; i < vec.size(); i++)
{
for (int j = 0; j < vec[0].size(); j++)
{
cout << "输出是" << vec[i][j] << endl;
}
}
return 0;
}
1 输入链表结点个数
2 输入链表的值
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
ListNode(int m)
{
m_nKey = m;
m_pNext = nullptr;
}
};
int main()
{
int n;
while(cin >> n)
{
vector<int> vec;
ListNode* dummynode = new ListNode(0);
ListNode* cur_node = dummynode;
while(n--)
{
int cur;
cin >> cur;
ListNode* next_node = new ListNode(cur);
cur_node->m_pNext = next_node;
cur_node = cur_node->m_pNext;
}
}
}