将字符串的元素存储到不同的向量/容器c++; 问候语我都很喜欢C++,但我很喜欢它(我是从C做起的)。这是一个IM系统的第三程序部分,我在寻找一个例子来帮助一个函数,所以我有一个小问题,我已经经历了很多例子,不能准确地找到我想要的东西。这可能是显而易见的,但我只是有一个大脑放屁。 我想读取一个布局如下的.txt文件: 类别、项目、价格、项目编号 main,steak bernaise,15,101 pudding,banoffee,3.99,102 starter,prawn cocktail,2.89,103 drink,gin & tonic,3.50,104
然后我想检测“,”并将不同的元素放入不同的向量中,我尝试了一些事情,比如getline,ispunt(价格上的句号使其不可行)和isspace一样,我确实想对这些元素使用“忽略”,但我认为列出这么多是不好的做法,除非我能定义我只想让isspunt检查”。请随意修改我的示例或提供您自己的示例将字符串的元素存储到不同的向量/容器c++; 问候语我都很喜欢C++,但我很喜欢它(我是从C做起的)。这是一个IM系统的第三程序部分,我在寻找一个例子来帮助一个函数,所以我有一个小问题,我已经经历了很多例子,不能准确地找到我想要的东西。这可能是显而易见的,但我只是有一个大脑放屁。 我想读取一个布局如下的.txt文件: 类别、项目、价格、项目编号 main,steak bernaise,15,101 pudding,banoffee,3.99,102 starter,prawn cocktail,2.89,103 drink,gin & tonic,3.50,104,c++,string,file,vector,C++,String,File,Vector,然后我想检测“,”并将不同的元素放入不同的向量中,我尝试了一些事情,比如getline,ispunt(价格上的句号使其不可行)和isspace一样,我确实想对这些元素使用“忽略”,但我认为列出这么多是不好的做法,除非我能定义我只想让isspunt检查”。请随意修改我的示例或提供您自己的示例 class Food { private: string category; string item; string price; string itemnum; public: string tostrin
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;
}
类食品{
私人:
字符串类别;
字符串项;
串价;
字符串itemnum;
公众:
字符串tostring();
食物(串猫,串它,串公关,串它)
:类别(cat)、项目(it)、价格(pr)、项目编号(itno){}
无效显示(ostream和输出)常数{
输出执行此操作的一种简单方法可能是使用std::stringstream
与getline
组合,如下所示:
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
}
}
这里还有另一种方法-此函数split()
将逗号分隔的字符串拆分为字符串向量
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
vector<string> split(const string& s)
{
vector<string> v;
size_t current = 0;
while (current != string::npos) {
auto next = s.find_first_of(',', current);
if (next == string::npos) {
v.push_back(s.substr(current));
current = string::npos;
}
else {
v.push_back(s.substr(current, next - current));
current = next + 1;
}
}
return v;
}
// test the function
int main()
{
ifstream infile("bill.txt");
while(!infile.eof()) {
string s;
getline(infile, s);
auto words = split(s);
if (words.size() == 4) {
cout << words[0] << "|" << words[1] << "|" << words[2]<< "|" << words[3] << endl;
}
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
向量拆分(常量字符串和s)
{
向量v;
电流大小=0;
while(当前!=字符串::npos){
自动下一步=s.find_first_of(',',current);
if(next==string::npos){
v、 推回(s.substr(当前));
当前=字符串::npos;
}
否则{
v、 推回(s.substr(当前,下一个-当前));
电流=下一个+1;
}
}
返回v;
}
//测试功能
int main()
{
ifstream INFLE(“bill.txt”);
而(!infle.eof()){
字符串s;
getline(填充,s);
自动字=拆分;
if(words.size()==4){
cout而(!fin.eof())
可能是错误的:您有一个调试器,并且能够逐行检查您的程序,是吗?@πάνταῥεῖ 虽然(!fin.eof())确实有效,但我的两个示例都在文件中读取。它只是在放入容器之前或之后将不同的元素分隔开。不过,按照中的方法,阅读整行内容并分别解析。@zarmotix:No,while(!fin.eof())
肯定是错误的。这看起来很有希望,我可以使用简单的“push_back”列表来存储到我的容器中吗?或者你可以帮助我举一个过程的例子,thnaks for the fast reply!@zarmotixtoken
是一个表示文件中当前项的std::string
。例如,你可以用它做任何你想做的事情,可以说int i=stoi(token);
将其转换为int
(如果您知道您正在阅读int
),然后可以将其推回到向量中
,依此类推。非常感谢您的帮助,这就是我要找的。
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
}
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
vector<string> split(const string& s)
{
vector<string> v;
size_t current = 0;
while (current != string::npos) {
auto next = s.find_first_of(',', current);
if (next == string::npos) {
v.push_back(s.substr(current));
current = string::npos;
}
else {
v.push_back(s.substr(current, next - current));
current = next + 1;
}
}
return v;
}
// test the function
int main()
{
ifstream infile("bill.txt");
while(!infile.eof()) {
string s;
getline(infile, s);
auto words = split(s);
if (words.size() == 4) {
cout << words[0] << "|" << words[1] << "|" << words[2]<< "|" << words[3] << endl;
}
}
return 0;
}