C++ cout auto separator

后端 未结 7 1790
半阙折子戏
半阙折子戏 2020-12-13 17:36

I am wondering if there is way that std::cout automatically will insert some predefined value between printed sequences.

For example:

st         


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 17:50

    If you're just looking for a way to print a vector with its elements properly delimited (and without an extra delimiter at the end), try this:

    #include 
    #include 
    
    int main()
    {
      std::vector v{1, 2, 3};
    
      auto it = v.begin();
      if (it != v.end())
      {
        std::cout << *it;
        ++it;
      }
    
      for (; it != v.end(); ++it)
      {
        std::cout << ", " << *it;
      }
    }
    

提交回复
热议问题