C++ 在向量对中查找元素并输出c++;

C++ 在向量对中查找元素并输出c++;,c++,vector,C++,Vector,我有一对向量: typedef pair<string,int> is; vector<is> v; 我需要让用户输入一个名称并搜索向量,找到该名称并输出对应的int。如果用户输入“一”,我希望输入类型为1 我尝试了以下方法 struct comp_pair_int { bool operator()(const pair<string, int>& a, const string& b) { return (

我有一对向量:

typedef pair<string,int> is;
vector<is> v;
我需要让用户输入一个名称并搜索向量,找到该名称并输出对应的int。如果用户输入“一”,我希望输入类型为1

我尝试了以下方法

struct comp_pair_int
{
    bool operator()(const pair<string, int>& a, const string& b)
    {
        return (a.first < b);
    }
    bool operator()(const string& a, const pair<string, int>& b)
    {
        return (a < b.first);
    }
};
sort(v.begin(),v.end(),comparison);
if (binary_search(v.begin(), v.end(),
    "One", comp_pair_int()))
    cout << "Element found\n";
else
    cout << "Element not found";
struct comp\u pair\u int
{
布尔运算符()(常量对&a、常量字符串&b)
{
返回(a.第一次bool
结果,因此它不能提供足够的信息来获取您要查找的配对的值

实现这一点的惯用方法是使用,它将一个迭代器返回给您要查找的对,如下所示:

if (auto i = std::lower_bound(v.begin(), v.end(),
                              "One", comp_pair_int());
    i != v.end() && i->first == "One")   // lower bound actually found correct pair
       cout << "Element found with value " << i->second;
else
       cout << "Element not found";
auto it = std::find_if(v.begin(), v.end(), [&](const is& item) {
    return item.first == user_input;
});
if(it != v.end()) {
    int my_item = it->second;
}
else {
    // key not found
}
if(自动i=std::下限(v.begin(),v.end(),
“一”,comp_pair_int());
i!=v.end()&&i->first==“One”)//下限实际上找到了正确的对
cout只会给您一个
bool
结果,因此它不能提供足够的信息来获取您要查找的配对的值

实现这一点的惯用方法是使用,它将一个迭代器返回给您要查找的对,如下所示:

if (auto i = std::lower_bound(v.begin(), v.end(),
                              "One", comp_pair_int());
    i != v.end() && i->first == "One")   // lower bound actually found correct pair
       cout << "Element found with value " << i->second;
else
       cout << "Element not found";
auto it = std::find_if(v.begin(), v.end(), [&](const is& item) {
    return item.first == user_input;
});
if(it != v.end()) {
    int my_item = it->second;
}
else {
    // key not found
}
if(自动i=std::下限(v.begin(),v.end(),
“一”,comp_pair_int());
i!=v.end()&&i->first==“One”)//下限实际上找到了正确的对

cout如果向量未按键排序,则可以使用
std::find_。
看起来是这样的:

if (auto i = std::lower_bound(v.begin(), v.end(),
                              "One", comp_pair_int());
    i != v.end() && i->first == "One")   // lower bound actually found correct pair
       cout << "Element found with value " << i->second;
else
       cout << "Element not found";
auto it = std::find_if(v.begin(), v.end(), [&](const is& item) {
    return item.first == user_input;
});
if(it != v.end()) {
    int my_item = it->second;
}
else {
    // key not found
}

如果向量未按键排序,则可以使用
std::find_
看起来是这样的:

if (auto i = std::lower_bound(v.begin(), v.end(),
                              "One", comp_pair_int());
    i != v.end() && i->first == "One")   // lower bound actually found correct pair
       cout << "Element found with value " << i->second;
else
       cout << "Element not found";
auto it = std::find_if(v.begin(), v.end(), [&](const is& item) {
    return item.first == user_input;
});
if(it != v.end()) {
    int my_item = it->second;
}
else {
    // key not found
}

注意,如果未找到请求的项,则必须考虑
std::find_if()
返回
end
迭代器,例如:
auto it=std::find_if(…);如果(it!=v.end()){int my_item=it->second;…}else{/*做点别的…*/}
小心,你必须考虑到
std::find_if()
如果没有找到请求的项,就会返回
end
迭代器,例如:
auto it=std::find_if(…);如果(it!=v.end()){int my_item=it->second;…}其他{/*做其他事…*/}