C++ 如何根据特定条件在向量中使用count()函数
我有一个带有一些元素的struct类型的向量,并试图计算该向量对应列中elementvalue的出现次数。我知道如何计算一个简单的向量,例如字符串类型的向量。但是我被向量卡住了。有什么可能的解决方案或建议吗 示例代码:C++ 如何根据特定条件在向量中使用count()函数,c++,vector,struct,count,C++,Vector,Struct,Count,我有一个带有一些元素的struct类型的向量,并试图计算该向量对应列中elementvalue的出现次数。我知道如何计算一个简单的向量,例如字符串类型的向量。但是我被向量卡住了。有什么可能的解决方案或建议吗 示例代码: #include <iostream> #include <algorithm> #include <vector> #include <string> struct my_struct { std::string fir
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
struct my_struct
{
std::string first_name;
std::string last_name;
};
int main()
{
std::vector<my_struct> my_vector(5);
my_vector[0].first_name = "David";
my_vector[0].last_name = "Andriw";
my_vector[1].first_name = "Jhon";
my_vector[1].last_name = "Monta";
my_vector[2].first_name = "Jams";
my_vector[2].last_name = "Ruth";
my_vector[3].first_name = "David";
my_vector[3].last_name = "AAA";
my_vector[4].first_name = "Jhon";
my_vector[4].last_name = "BBB";
for(int i = 0; i < my_vector.size(); i++)
{
int my_count=count(my_vector.begin(), my_vector.end(),my_vector[i].first_name);
/*I need help to count the number of occerencess of each "First_name" in a vector
For example: First_Name:- David COUNT:- 2 ...and so on for each first_names*/
std::cout << "First_Name: " << my_vector[i].first_name << "\tCOUNT: " << my_count << std::endl;
}
return 0;
}
但是,对于字符串类型的向量,std::vector的代码也可以正常工作。见下文:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> my_vector;
my_vector.push_back("David");
my_vector.push_back("Jhon");
my_vector.push_back("Jams");
my_vector.push_back("David");
my_vector.push_back("Jhon");
for(int i = 0; i < my_vector.size(); i++)
{
int my_count = count(my_vector.begin(), my_vector.end(),my_vector[i]); //this works good
std::cout << "First_Name: " << my_vector[i] << "\tCOUNT: " << my_count << std::endl;
}
return 0;
}
如果使用正确的谓词,则必须使用std::count\u:
int my_count = std::count_if(my_vector.begin(), my_vector.end(),
[&](const my_struct& s) {
return s.first_name == my_vector[i].first_name;
});
在C++03中替换lambda的函子:
struct CompareFirstName
{
explicit CompareFirstName(const std::string& s) : first_name(s) {}
bool operator () (const my_struct& person) const
{
return person.first_name == first_name;
}
std::string first_name;
};
然后
int my_count = std::count_if(my_vector.begin(), my_vector.end(),
CompareFirstName(my_vector[i].first_name));
如果使用正确的谓词,则必须使用std::count\u:
int my_count = std::count_if(my_vector.begin(), my_vector.end(),
[&](const my_struct& s) {
return s.first_name == my_vector[i].first_name;
});
在C++03中替换lambda的函子:
struct CompareFirstName
{
explicit CompareFirstName(const std::string& s) : first_name(s) {}
bool operator () (const my_struct& person) const
{
return person.first_name == first_name;
}
std::string first_name;
};
然后
int my_count = std::count_if(my_vector.begin(), my_vector.end(),
CompareFirstName(my_vector[i].first_name));
您好@jarod42,请您指导我如何使用std::count\u if。我看到了演示,但有一些错误。错误:在“]”标记之前应为主表达式,在“const”之前应为主表达式。谢谢我正在使用gcc版本4.4.7 20120313 Red Hat 4.4.7-11 gcc“由于您没有访问C++11的权限,您必须使用函子而不是lamdba。这是完美的解决方案!!谢谢。您好@jarod42,请您指导我如何使用std::count\u if。我看到了演示,但有一些错误。错误:在“]”标记之前应为主表达式,在“const”之前应为主表达式。谢谢我正在使用gcc版本4.4.7 20120313 Red Hat 4.4.7-11 gcc“由于您没有访问C++11的权限,您必须使用函子而不是lamdba。这是完美的解决方案!!谢谢