2015-10-21 35 views
0
#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轉換爲開始和結束時間的數組(在這種情況下爲向量)。我試圖這樣做的方式是首先將原始字符串拆分爲多個字符串(用','分隔),然後放入矢量timeFrame中。下一步是分割這些較小的字符串,將它們轉換爲整數,並將它們放入startTimes向量或endTimes向量中。我使用setIntFrames這一步,但我不明白爲什麼它只能設置第一次幀。C++爲什麼getline()只能在我的函數的第一個實例上工作?

我很確定問題在於我的singleSplit()函數,但我不明白getline和stringstream足以解決這個問題。任何幫助表示讚賞。

+0

你的程序不能編譯。 – Downvoter

+0

您正在返回對'singleSplit'中的局部變量的引用。變量'item'在'singleSplit'中定義。代之以通​​過引用或其他方式傳遞。我不認爲你發佈了實際的代碼,也許是測試版本。它運行但給我一個記憶錯誤。 – Matt

+0

'singleSplit'確實是問題所在:它缺少一個return語句。您應該啓用所有編譯器警告,以避免這樣的簡單錯誤。 – emlai

回答

0

這適用於我。我在引用中更改了傳遞參數,然後在使用後將其推回。

#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 
+0

這工作完美,謝謝! –

相關問題