Home c++ Entering a set C++

Entering a set C++

Author

Date

Category

There is a task:
“The sequence of numbers are recorded in the input line. For each number, output the word yes (in a separate line), if this number was previously found in a sequence or NO, if not found”
Question: An indefinite number is introduced, and how to enter this indefinite number of numbers (you need to enter in the set “set”)
I will be grateful)


Answer 1, Authority 100%

If you read from the terminal, you can do as Harry said. We read the string, turn it into stream and read. Sales:

# include & lt; iostream & gt;
#Include & lt; String & GT;
#Include & lt; set & gt;
#Include & lt; sstream & gt;
Using Namespace STD;
INT MAIN () {
  String S;
  set & lt; int & gt; S;
  int n = 0;
  GetLine (CIN, S);
  ISTRINGSTREAM I (S);
  While (i & gt; & gt; n)
    if (s.count (n) == 0) {
      COUT & LT; & LT; "NO \ n";
      S.INSERT (N);
    }
    ELSE.
      COUT & LT; & LT; "YES \ n";
  Return 0;
}

For a file, you can try to read before the end of the file. If the file is not 1 line, then use the first method, if one, then it is possible:

# include & lt; fstream & gt;
#Include & lt; iostream & gt;
#Include & lt; String & GT;
#Include & lt; set & gt;
Using Namespace STD;
INT MAIN () {
  set & lt; int & gt; S;
  int n;
  IFStream I ("Input.txt");
  While (i & gt; & gt; n)
    if (s.count (n) == 0) {
      COUT & LT; & LT; "NO \ n";
      S.INSERT (N);
    }
    ELSE.
      COUT & LT; & LT; "YES \ n";
  Return 0;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions