目录
- 一. 单行单项数据
- 1. cin >>
- 2. get() 函数
- 3. getline() 函数
- 4. C 中的做法:
- 4.1 getchar()
- 4.2 scanf()
- 二 单行多项数据
- 三 多行多项数据
一. 单行单项数据
1. cin >>
头文件:#include <iostream>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16// Eg: int _int; // input(控制台的输入): 10 cin >> _int; // : _int = 10 char _char; // input: asd cin >> _char; // : _char = 'a' // 对于string类字符串,会在遇到空白、制表符、换行符等处停止。 string _string; // input: asd dsa cin >> _string; // : _string = "asd";
2. get() 函数
头文件**#include <iostream>**
get() 进行单字符读取,get()函数读取一个输入字符(包括数字、大小写字母、空白、换行符、制表符等,不进行转换,读到啥就是啥)
区别项 | 达到文件尾时函数的返回值 | 达到文件尾时函数的返回值 |
---|---|---|
输入特征 | 达到文件尾时函数的返回值 | 把函数返回值赋值给ch |
读取字符之后函数返回至 | 指向istream对象的引用 | 字符编码,int值 |
达到文件尾时函数的返回值 | 转换为 false | EOF |
常用的重载:
复制代码
1
2
3
4
5
6cin.get(ch) // ch:char型的引用,把值直接赋值给ch cin.get(ch, len + 1, 'a') // 第一个参数:字符串首地址; // 第二个参数:控制最大读取字符数,为存放字符串末尾的'',此参数要比读取的字符串大一; // 第三个参数:分割符,(默认为'n', 即省略了第三个参数)
get()函数 没有实现对string对象的直接读取。
复制代码
1
2
3
4
5
6
7
8
9
10// Eg: char ch; // input: asd cin.get(ch); // : ch = 'a'; char _char[10]; // input: sdsdsdafd cin.get(_char, 10 + 1, 'a'); // _char = "sdsdsd",之后光标停留在 'a' 处。 cout << _char << endl;
3. getline() 函数
头文件:
#include <iostream>
#include <string> // 对string对象直接的处理
getline()函数会自动丢弃换行符等分界符。
常用的重载:
复制代码
1
2
3cin.getline(char, len + 1, 'a'); cin.getline(char, len + 1);
对于string对象的字符串, getline()函数 定义在string头文件中。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12// Eg: char ch; char _char[10]; // input: sdsdsdafd cin.getline(_char, 10, 'a'); // _char = "sdsdsd",之后光标停留在 'f'处。 cout << _char << endl; cin.get(ch); cout << ch << endl; string _string; getline(cin, _string); getline(cin, _string, 'a');
4. C 中的做法:
4.1 getchar()
4.2 scanf()
复制代码
1
2
3
4// Eg: int _int; scanf("%d", &_int);
二 单行多项数据
istringstream
copy()
istream_iterator()
情况划分:分隔符的表示,个数是否已知(N)。
I: 分隔符为空白符,个数已知
II: 分隔符为空白符,个数未知
复制代码
1
2
3
4
5
6
7// istringstream + copy string temp; getline(cin, temp); istringstream is(temp); vector<string> _v; copy(istream_iterator<string>(is), istream_iterator<string>(), back_inserter(_v));
不使用copy函数的话:
对于 I:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15// cin>> /* input: 11 12 input: a b input: asd dsa */ // template <typename T> vector<T> _vs; T temp; for(int _n = 0; _n < N; ++_n) { cin >> temp; _vs.push_back(temp); }
对于 II:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16// getline() && class istringstream // #include <sstream> /* input: a bc def ghij */ string temp; getline(cin, temp); istringstream is(temp); string part; vector<string> _v; while(is >> part) { _v.push_back(part); cout << part << endl; }
III: 分隔符不为空白符,个数已知
IV: 分隔符不为空白符,个数未知
复制代码
1
2
3
4
5
6
7
8
9
10string temp; getline(cin, temp); istringstream is(temp); string part; vector<string> result; while(getline(is, part, ';')) { result.push_back(part); }
三 多行多项数据
Eg1:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15/* input: [1,2,3,4,5,6] */ vector<int> result; string temp; cin.get(); getline(cin, temp, ']'); istringstream is(temp); string part; while(getline(is, part, ',')) { result.push_back(stoi(part)); }
Eg2:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28/* input: 3 1,10;32,45 78,94;5,16 80,100;200,220;16,32 */ int m; cin >> m; cin.get(); vector<pair<int, int> > result; for(int _m = 0; _m < m; ++_m) { string total; string part; getline(cin, total); istringstream is1(total); while(getline(is1, part, ';')) { istringstream is2(part); string s1; string s2; getline(is2, s1, ','); getline(is2, s2); result.push_back({stoi(s1), stoi(s2)}); } }
Eg3:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25/* 输入描述:第一行两个数,第一个数字表示用户的数目,第二个数字表示需要判断的用户序号。 第2 至 N+1 行表示序号为0到N-1的每个用户的朋友的序列列表。 例如: input 5 0 1 2 3 0 4 0 4 0 4 1 2 3 */ int N = 0; int m = 0; cin >> N >> m; cin.get(); vector<vector<int> > result(N); for(int _n = 0; _n < N; ++_n) { string temp; getline(cin, temp); istringstream is(temp); copy(istream_iterator<int>(is), istream_iterator<int>(), back_inserter(result[_n])); }
最后
以上就是淡然黑裤最近收集整理的关于C++面试输入整理一. 单行单项数据二 单行多项数据三 多行多项数据的全部内容,更多相关C++面试输入整理一.内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复