cin

PAT甲级1022(Digital Library)

橙三吉。 提交于 2020-02-06 08:00:44
使用结构: map<pair<int, string>, vector<int> > ; pair<int, string> :<标签,查询关键词> 遇到的一个坑点: 最后两个测试点,因id输出必须输出7位,用int存储并输出的需要前位补0 # include <bits/stdc++.h> using namespace std ; # define fin freopen("in.txt", "r", stdin) # define fout freopen("out.txt", "w", stdout) typedef pair < int , string > pis ; map < pis , vector < int > > mp ; int n ; void process_keyword ( string keywords , int id ) { string keyword = "" ; //分离Keyword for ( int i = 0 ; i < keywords . length ( ) ; i ++ ) { if ( keywords [ i ] == ' ' ) { mp [ make_pair ( 3 , keyword ) ] . push_back ( id ) ; keyword = "" ; } else { keyword =

Can a variable be initialized with an istream on the same line it is declared? [duplicate]

﹥>﹥吖頭↗ 提交于 2020-02-01 00:43:07
问题 This question already has answers here : Are there any tricks to use std::cin to initialize a const variable? (6 answers) Closed 2 years ago . Can the following two lines be condensed into one? int foo; std::cin >> foo; 回答1: The smart-ass answer: int old; std::cin >> old; The horrible answer: int old, dummy = (std::cin >> old, 0); The proper answer: old has to be defined with a declaration before it can be passed to operator>> as an argument. The only way to get a function call within the

Cin loop never terminating

馋奶兔 提交于 2020-01-30 08:35:05
问题 I'm having trouble getting my cin loop to terminate in my program. My program uses Linux redirection to read in input from the file hw07data, the data file look like this: 100 20 50 100 40 -1 A34F 90 15 50 99 32 -1 N12O 80 15 34 90 22 -1 The first portion is the total points for the class, the next lines are the students ID number followed by their scores, all terminated by -1. My issue: my while loop never terminates when i run the command ./a.out < hw07data , could anyone look over my code

Providing default value of cin input

拟墨画扇 提交于 2020-01-30 06:47:05
问题 My app reads user input using std::cin stream. In one place I would like to provide default input and let the user to accept it as it is (by pressing enter) or modify it before continuing (by removing old characters with backspace and adding new text). I'm aware that characters can be placed directly into cin.rdbuf , but that's not exactly what I want to achieve. I would like to put characters into console window in the place where console's cursor is when waiting for user input and do no

3752:走迷宫 DFS和BFS

邮差的信 提交于 2020-01-30 05:35:52
我是先用DFS写的 因为我没用bfs写过这种题 后来想了想bfs也得练练 就看了别人的写了一遍 注释很详细 BFS //第一次接触这个形式的bfs 有时候感觉用dfs要简单 //以前做的bfs都是关于图的 B # include <iostream> # include <queue> # include <string> using namespace std ; int n , m ; string s [ 100 ] ; int vis [ 100 ] [ 100 ] ; //用结构体来搞 以后每一层都是上一层的走的步数加1 struct f { int x , y ; int js ; } ; //比较巧妙的是 上下左右 用了一个数组来搞 //一开始我想错了 想成了和8连通似的 int dir [ 4 ] [ 2 ] = { - 1 , 0 , 1 , 0 , 0 , - 1 , 0 , 1 } ; int bfs ( int x , int y ) { //t1当之前的 t2是正准备走的 //一个也行 这样可能好理解一点吧 f t1 , t2 ; //一开始默认步数是1 t1 . x = x , t1 . y = y , t1 . js = 1 ; vis [ x ] [ y ] = 1 ; queue < f > q ; q . push ( t1 ) ; while

Why is istream.clear() removing part of my strings while reading doubles and strings?

一曲冷凌霜 提交于 2020-01-24 23:58:08
问题 I am reading a text file consisting of names and grades with the format: Polly 95 99 95 94 93 98 99 Abraham 80 85 81 86 95 78 81 I use the following code to read each student into a Student_info struct: // read homework grades from an input stream into a vector<double> istream& read_hw(istream& in, vector<double>& hw) { if (in) { // get rid of previous content hw.clear(); // read hw grades double grade; while (in >> grade) hw.push_back(grade); // clear the stream so that input will work for

Cin object return value c++ [duplicate]

谁说胖子不能爱 提交于 2020-01-24 19:05:27
问题 This question already has answers here : What is cin doing inside argument of if? [duplicate] (2 answers) Closed 6 years ago . I woud like to ask what is return value of cin? I know it is istream object and when it is used in expression like if(!cin) there is actually called some function and I woud like to know what function it actually is. cin.fail() or cin.good() or.. Is if(!cin) same as if(cin.fail())? 回答1: Yes. cin overloads casting operators, and they return flag status fail() . A

Cin object return value c++ [duplicate]

安稳与你 提交于 2020-01-24 19:03:38
问题 This question already has answers here : What is cin doing inside argument of if? [duplicate] (2 answers) Closed 6 years ago . I woud like to ask what is return value of cin? I know it is istream object and when it is used in expression like if(!cin) there is actually called some function and I woud like to know what function it actually is. cin.fail() or cin.good() or.. Is if(!cin) same as if(cin.fail())? 回答1: Yes. cin overloads casting operators, and they return flag status fail() . A

getline(cin, aString) receiving input without another enter

冷暖自知 提交于 2020-01-21 07:13:11
问题 My code looks like this, string aString; cin >> aString; cout << "This is what cin gets:" << aString << endl; getline(cin, aString); cout << "This is what getline(cin, <string>) gets:" << aString << endl; Each time I ran it, I give inputs like, "12", I get "12" and "". I am wondering why getline would received without user input. I can understand when I enter something like "12 24", cin will get "12", and getline should get the rest. (Also, if one could answer, the space in between is treated

ios_base::sync_with_stdio(false) does not work between two inputs from stdin

喜欢而已 提交于 2020-01-17 05:23:27
问题 I am wondering why does the following code not work as expected: #include <iostream> #include <string> using namespace std; int main(){ int n; string s; //scanf("%d",&n); cin >> n; ios_base::sync_with_stdio(false); cin >> s; cout << n << " " << s; return 0; } Input: 10 abcd Output: 10 The string is not getting printed! The result is same if I use scanf to input the integer. However, the code does work as expected if the line ios_base::sync_with_stdio(false); is placed before the first cin (or