There is a task: "The input line contains a sequence of numbers separated by a space. For each number, print the word YES (on a separate line), if that number was previously encountered in the sequence, or NO, if it was not met" Question: An unspecified number of numbers is entered, but how enter this indefinite number of numbers (you need to enter in the set of "set") I will be grateful)
Closed due to the fact that off-topic participants freim , LFC , Stranger in the Q , 0xdb , mkkik March 18 at 11:24 .
It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:
- " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- freim, LFC, Stranger in the Q, 0xdb, mkkik
|
1 answer
If you read from the terminal, then you can do as Harry said. We read the line, turn it into a stream and read it. Implementation:
#include <iostream> #include <string> #include <set> #include <sstream> using namespace std; int main() { string s; set <int> S; int n = 0; getline(cin, s); istringstream i(s); while (i >> n) if (S.count(n) == 0) { cout << "NO\n"; S.insert(n); } else cout << "YES\n"; return 0; }
For a file, you can try to read to the end of the file. If the file is not 1 line, then use the first method, but if it is one, then you can:
#include <fstream> #include <iostream> #include <string> #include <set> using namespace std; int main() { set <int> S; int n; ifstream i("input.txt"); while (i >> n) if (S.count(n) == 0) { cout << "NO\n"; S.insert(n); } else cout << "YES\n"; return 0; }
|
istringstream
. - Harry