将字符串的元素存储到不同的向量/容器c ++中 [英] store elements of a string into different vectors/containers c++

查看:109
本文介绍了将字符串的元素存储到不同的向量/容器c ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候所有im新的到c ++,但享受它(我来自做c)这是一个系统的第三个程序的一部分建设和寻找示例代码只是帮助一个函数,所以我有一个小问题,我已经经历了很多例子,无法找到我想要的,这可能是显而易见的,但只是有一个大脑放屁与它。
我想读取一个.txt文件,其布局为:
category,item,price,itemnumber

Greetings all im pretty new to c++ but enjoying it alot (i come from doing c) this is a third program part of a system im building and im looking for example code to just help with a function so i have a small question, ive been going through alot of examples and cant find exactly what i want, this may be obvious but im just having a brain fart with it. I want to read a .txt file with a layout of: category, item, price, itemnumber

main,steak bernaise,15,101
pudding,banoffee,3.99,102
starter,prawn cocktail,2.89,103
drink,gin & tonic,3.50,104

然后我想检测','并将不同的元素放入单独的向量,ive尝试了几个东西,如getline,ispunct(在价格的完全停止使这不可行)与isspace相同,我没有想过使用一个'忽略'与这些,但认为这种糟糕的做法列出这么多,除非我可以定义我只想让isspunt检查','。请自行修改我的示例或提供自己的示例。

i then want to detect ',' and put the different elements into seperate vectors, ive tried a few things such as getline, ispunct (the full stops in price make this non viable) same with isspace, i did wonder about using an 'ignore' with these but think thats bad practise to list so much unless i can define i only want isspunt to check ','. feel free to modify my examples or provide your own.

class Food {
private:
string category;
string item;
string price;
string itemnum;
public:
string tostring();
Food(string cat, string it, string pr, string itno)
    : category(cat), item(it), price(pr), itemnum(itno) {  }

void display(ostream& output) const{
    output << category << " " << item << " " << price << " " << itemnum << endl;
}
};

ostream& operator<<(ostream& output, Food const& f){
f.display(output);
return output;
}

以上是im类的使用, / p>

This above is the class im using, ive attached the overloader with this.

void filein::intake() {
string tempnum;
ifstream fin("bill.txt");
if(fin) {
    while(!fin.eof()) {
        string itemnum;
        string category;
        string item;
        string price;
        getline(fin, category, ',');
        getline(fin, item, ',');
        getline(fin, price, ',');
        getline(fin, itemnum);
        _items.push_back(Food(category, item, price, itemnum));
            }
    }
    for (size_t i = 0; i < _items.size(); ++i){
        cout << _items[i];
    }
}

这只是存储到1,我的示例可以修改为感激。

this is only storing into 1 though, if any of my examples can be modified id be grateful.

vector<string> split(const string& s)
{
vector<string> _totals;
vector<string> _items;
typedef string::size_type string_size;
string_size i = 0;

    // invariant: we have processed characters [original value of i, i) 
    while (i != s.size()) {
    // ignore leading blanks
    // invariant: characters in range [original i, current i) are all spaces
    while (i != s.size() && ispunct(s[i]))
     ++i;

    // find end of next word
     string_size j = i;
     // invariant: none of the characters in range [original j, current j)is a space
    while (j != s.size() && !ispunct(s[j]))
     j++;

     // if we found some nonpunctuation characters 
     if (i != j) {
     _totals.push_back(s.substr(i, j - i));


     i = j;
  }
 }
return _totals;
}

这不是工作,这是ispunct事情,我是sying,它,因为我想知道如果somwhere在while循环中,我可以添加行来解析字符串的不同元素到独立的向量。

this doesnt work either this was the ispunct thing i was sying about, i tried it as i wondered if somwhere in the while loops i could add lines to parse the different elements of the string into seperate vectors.

这样重申我的问题:我想要逐行读取文件从每一行的每个元素并存储在向量中,布局总是相同的。

so to reiterate my question: I want to read the file line by line take each element from that line and store in a vector, layout is always the same in the .txt but the words are different sizes they are always seperated by a ',' though.

感谢您提供任何帮助。

推荐答案

一个简单的方法是使用 std :: stringstream 结合 getline 像这样:

A simple way of doing this is probably using a std::stringstream in combination with getline like this:

std::string str;
while (std::getline(fin, str)) // fin is the input stream
{
    std::istringstream iss(str);
    std::string token;
    while (std::getline(iss, token, ',')) // get the token as a std::string
    {
         // process it here, can use `stoi` to convert to int
    }
}

这篇关于将字符串的元素存储到不同的向量/容器c ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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