C ++比较并替换stringstream的最后一个字符 [英] C++ Compare and replace last character of stringstream

查看:667
本文介绍了C ++比较并替换stringstream的最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查以下内容:


  1. 如果最后一个字符附加到 stringstream 是逗号。

  2. 如果将其删除。

  1. If the last character appended to the stringstream is a comma.
  2. If it is remove it.







std::stringstream str;
str << "["
//loop which adds several strings separated by commas

str.seekp(-1, str.cur); // this is to remove the last comma before closing bracket

str<< "]";

问题是如果循环中未添加任何内容,则从字符串中删除开括号。因此,我需要一种方法来检查最后一个字符是否为逗号。我这样做是这样的:

The problem is if nothing is added in the loop, the opening bracket is removed from the string. So I need a way to check whether the last character is a comma. I did that like this:

if (str.str().substr(str.str().length() - 1) == ",")
{
    str.seekp(-1, rteStr.cur);
}

但是我对此不太满意。

But I don't feel very good about this. Is there a better way to do this?

关于循环:

循环用于标记集合通过套接字接收的命令集,并将其格式化以通过另一个套接字发送到另一个程序。每个命令以 OVER 标志结尾。

Loop is used to tokenize a set of commands received through sockets and format it to send to another program through another socket. Each command ends with an OVER flag.

std::regex tok_pat("[^\\[\\\",\\]]+");
std::sregex_token_iterator tok_it(command.begin(), command.end(), tok_pat);
std::sregex_token_iterator tok_end;
std::string baseStr = tok_end == ++tok_it ? "" : std::string(*tok_it);
while (baseStr == "OVER")
{
    //extract command parameters
    str << "extracted_parameters" << ","
}


推荐答案

我经常处理这些循环的方式,希望在这些循环中放置空格或空格项目之间的逗号 如下所示:

The way I often deal with these loops where you want to put something like a space or a comma between a list of items is like this:

int main()
{
    // initially the separator is empty
    auto sep = "";

    for(int i = 0; i < 5; ++i)
    {
        std::cout << sep << i;
        sep = ", "; // make the separator a comma after first item
    }
}

输出:

0, 1, 2, 3, 4

如果想提高速度效率,可以使用 if( ),然后进入循环以输出其余的类似项:

If you want to make it more speed efficient you can output the first item using an if() before entering the loop to output the rest of the items like this:

int main()
{
    int n;

    std::cin >> n;

    int i = 0;

    if(i < n) // check for no output
        std::cout << i;

    for(++i; i < n; ++i) // rest of the output (if any)
        std::cout << ", " << i; // separate these
}

在您的情况下,第一个解决方案可以像此:

    std::regex tok_pat("[^\\[\\\",\\]]+");
    std::sregex_token_iterator tok_it(command.begin(), command.end(), tok_pat);
    std::sregex_token_iterator tok_end;
    std::string baseStr = tok_end == ++tok_it ? "" : std::string(*tok_it);

    auto sep = ""; // empty separator for first item

    while (baseStr == "OVER")
    {
        // extract command parameters
        str << sep << "extracted_parameters";
        sep = ","; // make it a comma after first item
    }

第二个(可能更节省时间)解决方案:

    std::regex tok_pat("[^\\[\\\",\\]]+");
    std::sregex_token_iterator tok_it(command.begin(), command.end(), tok_pat);
    std::sregex_token_iterator tok_end;
    std::string baseStr = tok_end == ++tok_it ? "" : std::string(*tok_it);

    if (baseStr == "OVER")
    {
        // extract command parameters
        str << "extracted_parameters";
    }

    while (baseStr == "OVER")
    {
        // extract command parameters
        str << "," << "extracted_parameters"; // add a comma after first item
    }

这篇关于C ++比较并替换stringstream的最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆