<i id='sSWhM'><tr id='sSWhM'><dt id='sSWhM'><q id='sSWhM'><span id='sSWhM'><b id='sSWhM'><form id='sSWhM'><ins id='sSWhM'></ins><ul id='sSWhM'></ul><sub id='sSWhM'></sub></form><legend id='sSWhM'></legend><bdo id='sSWhM'><pre id='sSWhM'><center id='sSWhM'></center></pre></bdo></b><th id='sSWhM'></th></span></q></dt></tr></i><div id='sSWhM'><tfoot id='sSWhM'></tfoot><dl id='sSWhM'><fieldset id='sSWhM'></fieldset></dl></div>

<small id='sSWhM'></small><noframes id='sSWhM'>

  • <legend id='sSWhM'><style id='sSWhM'><dir id='sSWhM'><q id='sSWhM'></q></dir></style></legend>
    <tfoot id='sSWhM'></tfoot>

          <bdo id='sSWhM'></bdo><ul id='sSWhM'></ul>

        如何以简单的方式从 C++ 的循环中删除最后一个逗号?

        How can I remove the last comma from a loop in C++ in a simple way?(如何以简单的方式从 C++ 的循环中删除最后一个逗号?)
        <legend id='zdgjT'><style id='zdgjT'><dir id='zdgjT'><q id='zdgjT'></q></dir></style></legend>
        • <i id='zdgjT'><tr id='zdgjT'><dt id='zdgjT'><q id='zdgjT'><span id='zdgjT'><b id='zdgjT'><form id='zdgjT'><ins id='zdgjT'></ins><ul id='zdgjT'></ul><sub id='zdgjT'></sub></form><legend id='zdgjT'></legend><bdo id='zdgjT'><pre id='zdgjT'><center id='zdgjT'></center></pre></bdo></b><th id='zdgjT'></th></span></q></dt></tr></i><div id='zdgjT'><tfoot id='zdgjT'></tfoot><dl id='zdgjT'><fieldset id='zdgjT'></fieldset></dl></div>

                <bdo id='zdgjT'></bdo><ul id='zdgjT'></ul>
                  <tbody id='zdgjT'></tbody>
                <tfoot id='zdgjT'></tfoot>
                • <small id='zdgjT'></small><noframes id='zdgjT'>

                • 本文介绍了如何以简单的方式从 C++ 的循环中删除最后一个逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这个程序用于打印素数直到给定的输入并用逗号分隔每个素数.

                  This program is for printing prime numbers till the input given and separating every prime number with a comma.

                  void main(){
                  
                      int N, counter=0, isPrime;
                  
                      int k, j;
                  
                      cout << "Enter maximum range: ";
                  
                      cin >> N;
                  
                      for (j=2; j<=N; j++){
                  
                          isPrime = 0;
                          k = 2;
                  
                          while (k<j){
                  
                              if (j%k==0){
                  
                                  isPrime++;
                              }
                              k++;
                          }
                          if (isPrime==0){
                  
                              if (k==N){
                                  cout << j;
                              }
                              else{
                                  cout << j << ",";
                              }
                              counter++;
                          }
                      }
                      cout << endl;
                      system("pause");
                  }
                  

                  它只删除素数输入的最后一个逗号,而不删除任何其他输入.我该如何解决这个问题?

                  It is only removing the last comma for prime number inputs, not for any other input. How can I fix this?

                  Input: 23
                  Output: 2,3,5,7,11,13,17,19,23
                  Input: 8
                  Output: 2,3,5,7,
                  Input: 9
                  Output: 2,3,5,7,
                  

                  推荐答案

                  没必要if then else那么多:

                  std::string delim = "";
                  for( auto&& item : vec )
                  {
                     std::cout << delim << item;
                     delim = ",";
                  }
                  

                  不需要对所有情况进行检查,例如向量是否为空.

                  No checking is needed for all cases, like the vector is empty or not.

                  如果在开头接受一个额外的空格,只需将字符串替换为char,那么性能会得到更大的提升.

                  If you accept an extra space in the beginning, just replace the string to char, and then the performance will be improved even more.

                  这篇关于如何以简单的方式从 C++ 的循环中删除最后一个逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                  Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)
                • <tfoot id='M89Yy'></tfoot>
                      <bdo id='M89Yy'></bdo><ul id='M89Yy'></ul>
                      <i id='M89Yy'><tr id='M89Yy'><dt id='M89Yy'><q id='M89Yy'><span id='M89Yy'><b id='M89Yy'><form id='M89Yy'><ins id='M89Yy'></ins><ul id='M89Yy'></ul><sub id='M89Yy'></sub></form><legend id='M89Yy'></legend><bdo id='M89Yy'><pre id='M89Yy'><center id='M89Yy'></center></pre></bdo></b><th id='M89Yy'></th></span></q></dt></tr></i><div id='M89Yy'><tfoot id='M89Yy'></tfoot><dl id='M89Yy'><fieldset id='M89Yy'></fieldset></dl></div>
                        <legend id='M89Yy'><style id='M89Yy'><dir id='M89Yy'><q id='M89Yy'></q></dir></style></legend>
                          • <small id='M89Yy'></small><noframes id='M89Yy'>

                              <tbody id='M89Yy'></tbody>