从文本文件读取并保存到列表
Reading From Text File and Save to List
Im 试图读取名为stock.txt
的文件,该文件包含以下值:
ID、项目、颜色、尺寸、数量、价格
11,T-shirt,blue,XL,2,10.500000
12,Supreme,red,M,10,20.500000
13,BANG,red,M,10,20.500000
我想将每个项目存储在矢量列表中,我该怎么做?
int main() {
ifstream infile;
infile.open("Stock.txt");
string id; string title; string colour; string size; string quantity; string cost;
//If file open is successful
while(infile.good()){
getline(infile,id,',');
getline(infile,title,',');
getline(infile,colour,',');
getline(infile,size,',');
getline(infile,quantity,',');
getline(infile,cost,'n');
}
infile.close();
}
然后,目标是实现一个搜索功能,我可以在其中搜索该列表中的特定项目(例如,通过使用 ID 或标题(
有两种常见的方法可以将元素添加到std::vector
。一个是.push_back()
,另一个是.emplace_back()
.push_back()
将构造对象,然后将其复制到向量中。emplace_back()
将直接在容器中构造对象。
我不完全确定你的意思
我想将每个项目存储在矢量列表中
但我会告诉你几种不同的方法。
字符串向量
由于看起来您拥有所有内容作为std::string
,因此您可以继续制作字符串向量。但是,您有许多实际上不应该是字符串的数值。在下面的部分中,我将提供一些策略,以便能够将这些值维护为数字。
但是,在这里,如果您希望每个条目在向量中都有自己的元素,则您的代码将变为
int main()
{
ifstream infile;
infile.open("Stock.txt");
string id, title, color, size, quantity, cost;
vector<string> all_data; // Create vector
//If file open is successful
while(getline(infile, id, ','))
{
getline(infile, title, ',');
getline(infile, color, ',');
getline(infile, size, ',');
getline(infile, quantity, ',');
getline(infile, cost, 'n');
// Add to vector
all_data.push_back(title);
all_data.push_back(color);
all_data.push_back(size);
all_data.push_back(quantity);
all_data.push_back(cost);
}
infile.close();
}
请注意,我确实更改了您的一些原始代码。
如果你想做一个大字符串作为一个元素,你也可以这样做:
int main()
{
ifstream infile;
infile.open("Stock.txt");
string id, title, color, size, quantity, cost;
vector<string> all_data; // Create vector
//If file open is successful
while(getline(infile, id, ','))
{
getline(infile, title, ',');
getline(infile, color, ',');
getline(infile, size, ',');
getline(infile, quantity, ',');
getline(infile, cost, 'n');
// Make one big string
string new_element =
title + ' ' + color + ' ' + ' ' + size + ' ' +
quantity + ' ' + cost;
// Add the new element to the vector
all_data.push_back(new_element);
}
infile.close();
}
结构向量
可能是保留原始数据类型的最佳方法。使用此方法,将数据声明为结构,然后以这种方式提供所有内容。
struct Data
{
int id;
string title, color, size;
unsigned quantity;
double cost;
};
int main()
{
ifstream infile;
infile.open("Stock.txt");
string id, title, color, size, quantity, cost;
vector<Data> all_data; // Create vector of Data
//If file open is successful
while (infile.peek() != EOF)
{
int new_id;
unsigned new_quantity;
double new_cost;
getline(infile, id, ',');
getline(infile, title, ',');
getline(infile, color, ',');
getline(infile, size, ',');
getline(infile, quantity, ',');
getline(infile, cost, 'n');
// Convert some datatypes
new_id = stoi(id);
new_quantity = static_cast<unsigned>(stol(quantity));
new_cost = stod(cost);
// Add new element to vector
all_data.emplace_back(Data{ new_id, title, color, size, new_quantity, new_cost });
}
infile.close();
}
如果有什么我可以澄清的,请告诉我。
在数据结构和代码之间创建一些协同作用:
struct stock_item
{
std::string id;
std::string title;
std::string colour;
std::string size;
int quantity;
double cost;
};
一旦您能够从文件中的一行创建stock_item
,您就可以轻松地随时随地/以您想要的方式存储和操作它:
std::vector<stock_item> some_vector;
stock_item some_stock_item;
// ...
some_vector.push_back(some_stock_item);
// ...
for (auto const& item : some_vector) {
std::cout << "Item #" << item.id << 'n';
}
所以你需要解析该文件:
auto stocks_from_stream(std::istream& in)
{
std::vector<stock_item> result;
std::string stock_line;
while(std::getline(in, stock_line)) {
auto item = stock_from_string(stock_line);
result.push_back(item);
}
return result;
}
然后,您需要定义此stock_from_string
函数:
auto stock_from_string(std::string const& line)
{
stock_item result;
// parse your line here
return result;
}
瞧!
相关文章:
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- Pybind11:将元组列表从Python传递到C++
- 是否可以将图像列表保存到文件中?
- 从文本文件读取并保存到列表
- clang格式:如何将构造函数的初始值设定项列表的每个元素保存在单独的行上
- 声明变量以保存字符串列表时的内存分配
- 考试C++保存列表的名称属性
- 模板中的编辑列表不保存
- 将 QPixmaps 列表保存到.ico文件
- 方法,该方法调用所有基类的同名方法(如果存在),并将返回值保存到列表中
- 如何将模板化类放入列表中并保存类型
- Boost.Graph:保存并加载一个名为Boost::adjacety_list的列表
- C#数组列表的C++等价物,用于保存多个数据类型
- 如何在列表中保存QWidget上绘制的点
- c++计算输入和保存点的结构与原点之间的距离,根据距离打印出列表
- 从B对象中访问类a实例的数据成员(保存类B对象的列表)
- 保存和恢复内存位置列表的清洁机制
- 如何保存对象(在这种情况下,球员)在一个列表中的c++
- 有没有一个网页,微软保存了在最新的VC++中实现的C++11功能的最新列表
- 在浏览项目列表时保存多个项目的数据