C++ 为什么 getline() 只适用于我的函数的第一个实例?
C++ why does getline() only work on the first instance of my function?
#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='[=10=]'){
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],'[=10=]').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 转换为开始和结束时间的数组(在本例中为向量)。我尝试这样做的方式是首先将原始字符串拆分为多个字符串(由','分隔)然后放入向量时间帧中。下一步是拆分那些较小的字符串,将它们转换为整数,然后将它们放入 startTimes 向量或 endTimes 向量中。我在此步骤中使用了 setIntFrames,但我不明白为什么它只能设置第一个 timeFrame。
我很确定问题出在我的 singleSplit() 函数上,但我对 getline 和 stringstream 的了解不足以解决这个问题。感谢任何帮助。
这对我有用。我将传入的参数更改为引用,然后在使用后将它们推回。
#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
#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='[=10=]'){
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],'[=10=]').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 转换为开始和结束时间的数组(在本例中为向量)。我尝试这样做的方式是首先将原始字符串拆分为多个字符串(由','分隔)然后放入向量时间帧中。下一步是拆分那些较小的字符串,将它们转换为整数,然后将它们放入 startTimes 向量或 endTimes 向量中。我在此步骤中使用了 setIntFrames,但我不明白为什么它只能设置第一个 timeFrame。
我很确定问题出在我的 singleSplit() 函数上,但我对 getline 和 stringstream 的了解不足以解决这个问题。感谢任何帮助。
这对我有用。我将传入的参数更改为引用,然后在使用后将它们推回。
#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