C++常用的输入方法
在C++语言中,标准输入操作需要包含头文件 <iostream>
。
1. cin
cin可以连续从键盘读取多个数据,直到遇到分隔符(空格、Tab键和换行符)为止。
2. getline()
当我们需要读取包含空格的字符串时,cin读不全整个字符串。此时,可以使用getline()函数来解决问题。
使用getline()函数时,需要包含头文件<string>。getline()函数会读取整行内容,包括空格字符,直到遇到换行符为止。
函数原型:getline(istream& is, string& str, char delim)
;
(1)is
是输入流,这里是 cin
,表示从标准输入流中读取数据。
(2)str
是存储读取结果的字符串。
(3)delim
是分隔符,即当读取到这个字符时,getline
函数停止读取。
getline(cin,s)
;没有提供第三个参数(分隔符),以此该句的作用是从标准输入中读取一行文本,将其存储在字符串 s
中,直到用户按下回车键为止。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include<string> #include<iostream> #include<sstream>
string line; getline(cin, line); stringstream ss(line); string str; while (ss >> str) { cout << str << endl; }
|
3. 字符串流stringstream
stringstream 是 C++ 提供的一个字符串流,使用必须包含头文件 #include<sstream>。
从stringstream 流中的数据输入字符串到一个变量里,是以遇到空格跳到下一个字符串的这样的形式,连续读取的。
1 2 3 4 5 6 7
| stringstream ss; 1. ss.str(); 2. stringstream ss(str1); 3. ss.str(const string& s); 4. ss >> str2; 5. while(ss >> str2); 6. getline(stringtream,str,'c');
|
4.getchar()
getchar()函数用于从缓冲区中读取一个字符,常用于判断是否遇到换行符等场景。
1 2 3 4 5 6
| char ch; while ((ch = getchar()) != '\n') { cout << ch; } cout << endl;
|
链表的定义和输出入
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 29 30 31 32 33
| #include <iostream>
using namespace std;
struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} };
int main() { ListNode* dummyHead = new ListNode(-1); ListNode* tail = dummyHead; int n;
while (cin >> n) { if (getchar() == '\n') { break; } ListNode* node = new ListNode(n);
tail->next = node; tail = node;
}
return 0; }
|
二叉树的定义和输出入
直接从输入中获取二叉树的结点值
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include <iostream> #include <vector>
using namespace std;
struct TreeNode { int val; shared_ptr<TreeNode> left; shared_ptr<TreeNode> right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} };
int main() { int n; cin >> n; if (n == 0) { cout << endl; return 0; }
vector<TreeNode* > nodes(n); for (int i = 0; i < n; ++i) { int value; cin >> value; if (value == -1) { nodes[i] = nullptr; } else { nodes[i] = new TreeNode(value); } if (i > 0) { if (i % 2 == 1) { if (nodes[(i - 1) / 2]) { nodes[(i - 1) / 2]->left = nodes[i]; } } else { if (nodes[(i - 2) / 2]) { nodes[(i - 2) / 2]->right = nodes[i]; } } } }
return 0; }
|
数组转换成链表
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
| #include<iostream> #include<vector>
using namespace std;
struct TreeNode { int value; TreeNode* left; TreeNode* right; TreeNode(int x) : value(x), left(nullptr), right(nullptr) {} }; TreeNode* createTree(vector<int>& nums, int index, int n) { if (index >= n || nums[index] == -1) return nullptr; TreeNode* root = new TreeNode(nums[index]); if (index * 2 + 1 < n) root->left = createTree(nums, index * 2 + 1, n); if (index * 2 + 2 < n) root->right = createTree(nums, index * 2 + 2, n); return root; } int main() { vector<int> nums = {1, 2, 3, 4, 5, -1, -1, -1, -1, -1}; TreeNode* root = createTree(nums, 0, nums.size()); return 0; }
|