C ++为什么getline()只适用于我函数的第一个实例?

时间:2015-10-21 16:50:19

标签: c++11 getline

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>

//These two functions work fine
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        if(!item.empty())
            elems.push_back(item);
    }
    return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}

//I think this function is where the problem is
std::string& singleSplit(const std::string* s, char delim='\0'){
    static std::string input=*s;
    static std::stringstream ss(*s);
    if(input!=*s){ss.str(*s);input=*s;}

    std::string item;

    if (std::getline(ss, item, delim)&&!item.empty())
        {std::cout<<item<<std::endl;return item;}

}
void setIntFrames(std::vector<std::string>& frames, std::vector<uint16_t>* Start, std::vector<uint16_t>* End)
{
    for(int i=0; i<frames.size(); i++)
    {
        Start->push_back(std::atoi((singleSplit(&frames[i],'-').c_str())));
        End->push_back(std::atoi((singleSplit(&frames[i],'\0').c_str())));
    }//this loop works fine the first time it passes, but the second time it just pushes back 0's into my Start and End vectors
}


int main()
{
    std::string x="0000-1200,1201-2359";//sample string of what alloted time frames in a day would look like
    std::vector<std::string>timeFrames(split(x,','));
    std::vector<uint16_t>startTimes; std::vector<uint16_t>endTimes;
    setIntFrames(timeFrames, &startTimes, &endTimes);

    std::cout<<startTimes[1]<<std::endl<<endTimes[1]; 
//setIntFrames() didn't set these correctly, I don't know why
    return 0;
}

我试图制作一个简单的调度程序。我正在处理将main()内部的字符串x转换为开始和结束时间的数组(在本例中为向量)。我尝试这样做的方法是首先将原始字符串拆分为多个字符串(由&#39;,&#39;分隔)然后放入向量timeFrames。下一步是拆分那些较小的字符串,将它们转换为整数,然后将它们放入startTimes向量或endTimes向量中。我在这一步使用了setIntFrames,但我不明白为什么它只能设置第一个timeFrame。

我很确定问题出在我的singleSplit()函数中,但我不太了解getline和stringstream来解决这个问题。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

这对我有用。我将pass参数更改为引用,然后在使用后将其推回。

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>

//These two functions work fine
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        if(!item.empty())
            elems.push_back(item);
    }
    return elems;
}
std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}

//I think this function is where the problem is
void singleSplit(const std::string& s, char delim, std::string& first, std::string& second){

    std::stringstream ss(s);

    if (std::getline(ss, first, delim)){
    std::cout << "Found:" << first << '\n';
    }
    if (std::getline(ss, second)){
        std::cout << "Found:" << second << '\n';
    }

}
void setIntFrames(std::vector<std::string>& frames, std::vector<uint16_t>& Start, std::vector<uint16_t>& End)
{
    std::string first, second;
    for(int i=0; i<frames.size(); i++)
    {
        std::cout << frames[i] << '\n';
    singleSplit(frames[i], '-', first, second);
        Start.push_back(std::atoi(first.c_str()));
        End.push_back(std::atoi(second.c_str()));
    }//this loop works fine the first time it passes, but the second time it just pushes back 0's into my Start and End vectors
}


int main()
{
    std::string x="0000-1200,1201-2359";//sample string of what alloted time frames in a day would look like
    std::vector<std::string>timeFrames(split(x,','));
    std::vector<uint16_t>startTimes; std::vector<uint16_t>endTimes;
    setIntFrames(timeFrames, startTimes, endTimes);

    // std::cout<<startTimes[1]<<std::endl<<endTimes[1]; 
    for (int i = 0; i < startTimes.size(); ++i){
        std::cout << i << ':' << startTimes[i] << '\t' << endTimes[i] << '\n';
    }
//setIntFrames() didn't set these correctly, I don't know why
    return 0;
}

输出如下:

 $ a.out
0000-1200
Found:0000
Found:1200
1201-2359
Found:1201
Found:2359
0:0     1200
1:1201  2359