【问题标题】:c++ input till end of input signalled through keyboardc ++输入直到通过键盘发出信号的输入结束
【发布时间】:2013-07-28 19:24:42
【问题描述】:

我想编写一个 C++(C,如果它为我的问题提供简单的解决方案)程序,可以输入,直到他选择通过按 Ctrl+ 等按钮组合来表示输入结束 D.我对此有两个问题。

  1. 在 Xterm 中使用哪个组合键来表示输入结束? (Ctrl+C 或 Z 不起作用)
  2. 当按下 1 中回答的组合键时,我的 while() 循环中的逻辑代码应该是什么?

    map<int,string>info;
    string name;
    int age;
    cin>>name;
    while( ????????? ){   //Input till EOF , missing logic
        cin>>age;
        info.insert( pair<int,string>(age,name) );
        cin>>name;
    }
    //sorted o/p in reverse order
    map<int,string> :: iterator i;
    for(i=info.end(); i !=info.begin(); i--)
        cout<<(*i).second<<endl;
    cout<<(*i).second<<endl;
    

    }

程序在接收到来自终端的输入信号结束时继续。

我使用gcc/g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3

【问题讨论】:

    标签: c++ logic user-input xterm g++-4.7


    【解决方案1】:

    while 条件应该是这样的:

    while(the_key_combination_pressed_in_the_last_loop!=what_combination_will_exit_while)
    {
      cin>>age;
      if(age!=what_combination_will_exit_while)
      {
          info.insert( pair<int,string>(age,name) );
          cin>>name;
      }
    }
    

    【讨论】:

      【解决方案2】:

      使用istream_iterator

      等待EOF的默认构造函数

      Ctrl+ZF6+ENTER 在 Windows 上

      Ctrl+D 在 Linux 上

      我会使用代理类即时插入地图,如下所示:

      #include <map>
      #include <iterator>
      #include <algorithm>
      #include <string>
      #include <functional>
      #include <iostream>
      
      template<class Pair>
      class info_reader // Proxy class, for overloaded << operator
      {
      public:
              typedef Pair pair_type;
      
              friend std::istream& operator>>(std::istream& is, info_reader& p)
              {
                    return is >> p.m_p.first >> p.m_p.second;
              }
      
              pair_type const& to_pair() const
              {
                      return m_p; //Access the data member
              }
      private:
              pair_type m_p;                
      };
      
      
      int main()
      {
          typedef std::map<int, std::string> info_map;
      
          info_map info;
          typedef info_reader<std::pair<int, std::string> > info_p;
      
             // I used transform to directly read from std::cin and put into map
              std::transform(
                  std::istream_iterator<info_p>(std::cin),
                  std::istream_iterator<info_p>(),
                  std::inserter(info, info.end()),
                  std::mem_fun_ref(&info_p::to_pair) //Inserter function
                      );
      
      //Display map
      for(info_map::iterator x=info.begin();x!=info.end();x++)
         std::cout<<x->first<< " "<<x->second<<std::endl;
      }
      

      【讨论】:

      • 插入地图对我来说不是什么大问题。我想要一个像 while(cin&gt;&gt;name){...} 这样的 c++ 逻辑 smthng,它会在按下 Key-Combination 后立即停止处理输入。但显然这行不通。所以建议我使用其他方法。
      • @KunalKrishna 上面的代码将与Ctrl+D (linux) 或Ctrl+Z 一起使用你想要任何其他组合键吗?
      • @POW 我得到了第一部分 wrt 组合键的答案。但我很难实施它。问题是使用相同的构造,如果它是字符串,我必须做任何事情,如果它是那个组合键,我必须做 exit
      猜你喜欢
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多