分隔由“00”分隔的字符串向量(或数组)

时间:2016-04-20 19:16:12

标签: c++ string

(C ++)我有一个不同“类别”的字符串向量。每个类别在向量中用“00”字符串分隔。

所以喜欢:

“披萨” “冰淇淋” “面包” “00” “视窗” “苹果电脑” “LINUX” “UNIX” “00” “下雨” “下雪” “00” “徒步旅行” “00”

我想弄清楚如何将这些类别分成一个字符串向量,所有类别都在一个字符串中,如:

“披萨冰淇淋面包” “Windows Mac Linux Unix” “下着大雨” “远足”

编辑:alldoubles是矢量名称,其大小为totalsize

尝试:尝试在字符串不是00时添加到队列

int loc = 0; //counter
    queue<string> tmp;
    string cur;
    while (loc<totalsize)
    {
        while (cur != "00")
        {
            tmp.push(cur); //add to queue when not 00
            loc = loc + 1;
            cur = alldoubles[loc];
        }

        while (tmp.empty()!=false)  //unload the queue into a string res 
        {                           // after an 00 was found and cout it
            string res;
            res = res + tmp.front();
            tmp.pop();
            cout << "RES " << res << endl;
        }
    }

编辑:现在我得到RES和RES Pizza,这不是所有食物的代码

int loc = 0;
queue<string> tmp;
string cur;
while (loc<totalsize)
{
    while (cur != "00")
    {
        tmp.push(cur);
        cur = alldoubles[loc];
        loc = loc+1;
    }

    while (!tmp.empty())
    {
        string res;
        res = res + tmp.front();
        tmp.pop();
        cout << "RES " << res << endl;
    }
}

4 个答案:

答案 0 :(得分:2)

while (tmp.empty()!=false)替换为while(!tmp.empty())。至少......然后你会在cout上得到一些东西。

此外,您需要在使用cur之前启动alldouble[0]

最后,res应该在最后一个while循环之外声明,然后,在循环执行之后,它应该包含你期望的内容。顺便说一下,在将元素连接成最终字符串时,不要忘记添加空格。

在检查&#39; 00&#39;时,还应该可以动态构建res。在第一个循环内。那时不再需要第二个while

答案 1 :(得分:2)

// Example program
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<std::string> vv = { "one", "00",  "two", "three",  "00",  "four" , "five", "six",  "00", "seven" };

    std::string const splitter("00");

    // !!!! add 00 to the end
    vv.push_back(splitter);

    auto bb = vv.begin();
    auto it = std::find(vv.begin(), vv.end(), splitter);

    // finds groups and prints them one by one
    while (it != vv.end())
    {
        std::string accumulator;
        for(auto it2 = bb; it2 != it; ++it2)
            accumulator += *it2;

        bb = ++it;

        it = std::find(bb, vv.end(), splitter);
        std::cout << accumulator << std::endl;
    }
}

打印:

one
twothree
fourfivesix
seven

答案 2 :(得分:1)

如果您有权访问C ++ 11,那么使用标准库中的algorithmiterator会更加惯用。

std::vector<std::string> stuff = {"Pizza", "IceCream", "Bread", "00", "Windows", "Linux", "Mac", "00", "Raining", "Snowing", "00"};
std::vector<std::vector<std::string>> results;

//find occurance of "00"
auto t = std::find(stuff.begin(), stuff.end(), "00");
//save previous position for copy starting point
auto t_old = stuff.begin(); 
while (t != stuff.end()) {
    //temporary vector to hold the strings in this segment
    auto accum = std::vector<std::string>( (t - t_old) );
    //copy from last "00" to next "00" into accum
    std::copy(t_old, t, accum.begin());
    //bump copy point to new location of next element after last found "00"
    t_old = t+1;
    //find next "00"
    t = std::find(t+1, stuff.end(), "00");
    results.push_back(accum);
}

这个解决方案并不完美,并没有尝试将字符串连接在一起,但它是避免许多潜在陷阱的良好替代方法。

答案 3 :(得分:1)

这是一个可能的解决方案:

int main()
{
    vector<string> vec { "Pizza", "IceCream", "Bread", "00",
                         "Windows", "Mac", "Linux", "Unix", "00",
                         "Raining", "Snowing", "Hiking", "00" };

    int count {};
    string cat1;
    string cat2;
    string cat3;
    for_each(begin(vec), end(vec), [&](string str) {
        if (str == "00") {
            ++count;
            return;
        }
        if (count == 0) {
            for (auto& c : str) 
                cat1.push_back(c);
            cat1.push_back(' ');
        }
        if (count == 1) {
            for (auto& c : str) cat2.push_back(c);
            cat2.push_back(' ');
        }
        if (count == 2) {
            for (auto& c : str) cat3.push_back(c);
            cat3.push_back(' ');
        }
    });

    cout << cat1 << '\n';
    cout << cat2 << '\n';
    cout << cat3 << '\n';
}