在结构向量中搜索一个数据成员,然后打印匹配的所有数据成员
Searching a vector of structs for one data member, then printing all data members that match
>我正在尝试搜索csv文件以打印以特定名称找到的数据。 我已经尝试了在这个网站上找到的多个建议,但由于我不断出现错误,似乎没有任何效果。可能是因为我不太明白为什么我的代码不起作用,所以我非常感谢解释。我的代码可以在下面找到:
code removed
首先,你的while循环条件没有错,但可以通过使用运算符bool来简化:
while(inFS)
{
...
}
其次,您对std::find_if
的使用不正确。第三个参数必须是签名bool(Record)
的可调用对象(可能是const
或 ref 限定的(,并将迭代器返回到第一个匹配项。
从您的变量名称和字符串中,我想您想搜索向量中的Records::recip
成员。std::find_if
是实现此目的的好方法:
auto comp = [&search](Record const& r) { return r.receip == search; };
for (auto it = std::find_if(input.begin(), input.end(), comp);
it != input.end();
it = std::find_if(++it, input.end(), comp))
{
// this loop iterates over all matches for search in Record::receip
Record& record = *it;
std::cout << record << 'n';
}
您使用find_if的方式不正确,您需要使用 lambda 在结构向量中进行自定义搜索。这是更直接的方法:
for(const Record& record : input) // Iterate all records
{
if(record.recip == search) // Check if 'recip' of the record matches the requested one
{
cout << "found";
return 0;
}
}
!= string::npos
不是正确的条件。find_if
将迭代器返回到满足条件的第一个元素,如果未找到此类元素,则返回last
。last
在你的情况下input.end()
.
您按getline
读取字段,但在输入搜索字符串时,使用格式化输入(空格中断(。如果该字段在文件中包含空格,您将永远无法找到它。
若要简化输入/输出,请考虑将运算符<<
和>>
添加到类中。您当前的while(!inFS.fail())
条件不起作用。如果您从文件中读取最后一个Records
结构,则fail()
将不正确,因此您将尝试读取一个完整的Records
(但每一行都失败(。您仍会将错误的Records
结构添加到vector
中。
作为使用 lambda 的其他建议的替代方案(这很好(,您也可以在Records
中添加operator==
- 如果您只想能够搜索相同的东西。
另请阅读为什么"使用命名空间 std;"被认为是不好的做法?
例:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
struct Records {
std::string year{};
std::string category{};
std::string won{};
std::string recip{};
// added operator to be able to compare a Records with a string
bool operator==(const std::string& Recip) const { return recip == Recip; }
};
// custom streaming operator for reading one `Records` from a stream (like a file)
std::istream& operator>>(std::istream& is, Records& r) {
std::getline(is, r.year, ',') && std::getline(is, r.category, ',') &&
std::getline(is, r.won, ',') && std::getline(is, r.recip);
return is;
}
// custom streaming operator to write a `Records` to a stream
std::ostream& operator<<(std::ostream& os, const Records& r) {
return os << r.year << 'n' << r.category << 'n' << r.won << 'n' << r.recip << 'n';
}
int main() {
std::ifstream inFS("oscars.csv");
if(!inFS) { // in boolean context, inFS will be false if not opened
std::cout << "Failed to open file.n";
return 1;
}
std::vector<Records> input;
Records awardIn;
// use the custom operator>> to extract a "Records" from the file and push it back
// inFS will be false in boolean context if the extraction failed
while(inFS >> awardIn)
input.push_back(awardIn);
std::string search;
std::cout << "Enter recipient: ";
if(std::getline(std::cin, search)) { // check that reading succeed
// with the added operator== you can use a normal std::find
for(auto it = std::find(input.begin(), input.end(), search);
it != input.end();
it = std::find(std::next(it), input.end(), search))
{
// use the custom operator<< to print the Records
std::cout << *it << "n";
}
}
}
- 用于访问容器<T>数据成员的正确 API
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 数据成员SFINAE的C++17测试:gcc vs clang
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 静态数据成员模板专用化的实例化点在哪里
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 使用指针访问数组中的对象数据成员
- 友元函数无法访问私有数据成员 (c++)
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- 将私有数据成员添加到野牛生成的类中
- 输入数据成员未按要求工作
- 二维矢量数据成员
- 在类 A 中创建类型为 B 类的向量 - 访问数据 [C++] [成员在两个类中都是私有的]
- 调用在 HXX 文件中声明的静态数据成员
- 是否可以根据其数据成员的类型确定类型的大小
- 访问数据成员(本身是对象)的数据成员,就好像它们是类成员一样
- 在结构向量中搜索一个数据成员,然后打印匹配的所有数据成员
- 我应该如何打印出返回类型为char的string和int的数据成员?