代码之家  ›  专栏  ›  技术社区  ›  Meraj al Maksud SebastianX

在换行之前,我如何才能接受几个整数作为输入?[关闭]

  •  -2
  • Meraj al Maksud SebastianX  · 技术社区  · 6 年前

    我要几个 整数 作为输入直到 换行符(“\n”,不是eof) 发生,然后将它们放入 array vector . 如何使用更多的C++函数和数据结构呢?(更欢迎幼稚的实施)

    3 回复  |  直到 6 年前
        1
  •  3
  •   Thomas Matthews    6 年前

    #include <string>
    #include <sstream>
    #include <iostream>
    
    int main()
    {
      std::vector<int> database;
      std::string text_line;
      std::getline(std::cin, text_line);
      std::istringstream input_stream(text_line);
      int value;
      while (input_stream >> value)
      {
        database.push_back(value);
      }
    // Edit 1: printing the vector
      for (auto&& v:database)
      {
         std::cout << v << " ";
      }
      std::cout << "\n";
      return EXIT_SUCCESS;
    }
    

        2
  •  3
  •   Yakk - Adam Nevraumont    6 年前
    int main(){
      std::vector<int> arr;
    
      do {
        int v;
        char sep;
        auto code = std::scanf("%d%c", &v, &sep);
        if (code >= 1)
          arr.push_back(v);
        if (code < 2 || sep == '\n')
          break;
      } while(true);
    
      for (auto&& v:arr)
        std::printf("%d ", v);
    }
    

    std::scanf std::printf cin cout

    printf scanf

    std::cin >> x https://en.cppreference.com/w/cpp/locale/num_get/get

    . ,

    \t0x123 012 \t33 \n-3.-0x33\n
    

        3
  •  0
  •   wally    6 年前

    #include <iostream>
    #include <vector>
    
    int main(){
        std::vector<int> arr;
        int temp;
        while(std::cin >> temp)
            arr.push_back(temp);
        for(auto i : arr)
            std::cout << i << " ";
        std::cout << '\n';
    }
    

    EOF signal