如何删除最后一个逗号?

时间:2019-04-18 03:49:15

标签: c++

程序显示数字列表。  例如:

  

1、3、5、3、4、3,

我需要这样:

  

1、3、5、3、4、3

我尝试了if/else语句,但这没有用。

s.push_back(0);
cout << "s: ";
cout << 0 << ", ";
s.push_back(v[0]);
cout << s.back() << ", ";

for(int i=2; i<=10; i++) {
    s.push_back(alpha * v[i-1] + (1 - alpha) * s.back());
    if (i == s.back() - 1)
        cout << s.back() << endl;
    else
        cout << s.back() << ", " << "";

我得到正确的输出。只是有摆脱最后一个逗号的麻烦。

3 个答案:

答案 0 :(得分:4)

如果您将逗号放在除第一个元素之前的所有元素之前,您将实现

 if(first){
    out << n;
    first = false;
 }else{
    out << "," << n;
 }

答案 1 :(得分:3)

通过更新定界符变量来避免if

#include <iostream>
#include <vector>
#include <functional>

int main() {
  std::vector<int> example = {1,2,3,4,5};

  std::string delim = "";

  for(const auto& e:example){
    std::cout << delim << e;
    delim = ", ";
  }

  return 0;
}

答案 2 :(得分:2)

有三种方法:

  1. 在元素之前打印逗号,除非它是第一个:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table id="data" border="1" cellspacing="1" width="500">
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    
    <button type="button" id="change">Change</button>
  2. 相似,但没有布尔值:

    private void getDataWtd(String type, String url) {
    
        // instantiate and initialize volley requestQueue
        final RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    
        // instantiate jsonObjectRequest
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
    
                // extract timeSeries part of the JSON
                Log.i("test", "onResponse: "+response);
                JSONObject jsonObject = getTimeSeries(response);
    
                if(type.equals("intra")){
                    jsonObjectIntra = jsonObject;
    
                }else {
                    jsonObjectHistory = jsonObject;
                }
    
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("test", "Something went wrong: "+ error.getMessage());
                Toast.makeText(getActivity(), "Unable to perform request", Toast.LENGTH_LONG).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }
    
  3. 检查索引:

    bool first = true;
    for ...
       if (!first) 
           cout << ", ";
       cout << s.back();
       first = false;
    

    在每个元素的末尾打印逗号时,索引检查也可以在最后一个元素上进行。

  4. 在循环外打印第一个元素,其余元素将在循环中以定界符作为前缀(请注意,这不适用于空列表)。