C++ 在多维向量中搜索两个特定元素

C++ 在多维向量中搜索两个特定元素,c++,vector,C++,Vector,考虑以下向量 vector<vector<string>> a_words(80000,vector<string>(3)); 基本上我想按行搜索,我想找到Joan Williams,记住Joan是第一列中的一个元素,Williams是第二列中的一个元素 我应该使用“查找”功能吗?如果是,该如何编写,否则我应该使用哪个函数?我强烈建议您使用带有重载相等运算符的数据结构,而不是向量(特别是因为第三个元素似乎应该保存在整数中,而不是字符串中) 无论如何,这是一种

考虑以下向量

vector<vector<string>> a_words(80000,vector<string>(3));
基本上我想按行搜索,我想找到Joan Williams,记住Joan是第一列中的一个元素,Williams是第二列中的一个元素


我应该使用“查找”功能吗?如果是,该如何编写,否则我应该使用哪个函数?

我强烈建议您使用带有重载相等运算符的数据结构,而不是
向量(特别是因为第三个元素似乎应该保存在整数中,而不是字符串中)

无论如何,这是一种可能性:

auto iter = std::find_if( std::begin(a_words), std::end(a_words),
                          [] (std::vector<std::string> const& vec)
                          { return vec[0] == "Joan" && vec[1] == "Williams";};
自动iter=std::find_if(std::begin(a_字),std::end(a_字), [](标准::向量常量和向量) {return-vec[0]==“Joan”&&vec[1]==“Williams”;};

如果列表按第一列或第二列按字典顺序排序,则可以使用二进制搜索。

从C++11开始,基于范围的for循环将是一种简单易读的解决方案:

for(auto r: a_words)
    if(r[0] == "Joan" && r[1] == "Williams")
        cout << r[0] << " " << r[1] << " " << r[2] << endl;
for(自动r:a_字)
如果(r[0]=“Joan”&&r[1]=“Williams”)

CUT< P>这里有两个演示程序,一个C++ 2003,另一个C++ 2011,执行搜索< /P> C++2003

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>

struct FindName : std::unary_function<bool, 
                                      const std::pair<std::string, std::string>>
{
    FindName( const std::pair<std::string, std::string> &p ) : p( p ){} 
    bool operator ()( const std::vector<std::string> &v ) const
    {
        return v.size() > 1 && 
               v[0] == p.first && v[1] == p.second;
    }
protected:
    const std::pair<std::string, std::string> p;
};

int main()
{
    const size_t N = 5;
    std::vector<std::vector<std::string>> v;
    v.reserve( N );

    const char * initial[N][3] =
    {
        { "Joan", "Williams", "30" },
        { "Mike", "Williams", "40" },
        { "Joan", "Smith", "30" },
        { "William", "Anderson", "20" },
        { "Sara", "Jon", "33" }
    };

    for ( size_t i = 0; i < N; i++ )
    {
        v.push_back( std::vector<std::string>( initial[i], initial[i] + 3 ) );
    }

    std::pair<std::string, std::string> p( "Joan", "Williams" );

    typedef std::vector<std::vector<std::string>>::iterator iterator;

    iterator it = std::find_if( v.begin(), v.end(), FindName( p ) );

    if ( it != v.end() )
    {
        for ( std::vector<std::string>::size_type i = 0; i < it->size(); ++i ) 
        {
            std::cout << ( *it )[i] << ' ';
        }
    }
    std::cout << std::endl;
}
#包括
#包括
#包括
#包括
#包括
#包括
结构FindName:std::一元函数
{
FindName(const std::pair&p):p(p){}
布尔运算符()(const std::vector&v)const
{
返回v.size()>1&&
v[0]==p.first&&v[1]==p.second;
}
受保护的:
常数std::对p;
};
int main()
{
常数大小N=5;
std::向量v;
v、 储备(N);
常量字符*首字母[N][3]=
{
{“琼”,“威廉姆斯”,“30”},
{“迈克”,“威廉姆斯”,“40”},
{“琼”、“史密斯”、“30”},
{“威廉”,“安德森”,“20”},
{“莎拉”、“乔恩”、“33”}
};
对于(大小i=0;isize();++i)
{

STD::CUT

本质上,@ Columbo的答案很好,消除了C++ 11个特性(除了初始化):

#包括
#包括
#包括
#包括
int main(){
//需要C++11
标准::向量字={
{“琼”,“威廉姆斯”,“30”},
{“迈克”,“威廉姆斯”,“40”},
{“琼”、“史密斯”、“30”},
{“威廉”,“安德森”,“20”},
{“Sara”,“Jon”,“33”},
};
//下面不需要C++11
结构EqualName
{
常量字符*第一;
常量字符*秒;
EqualName(常量字符*第一个,常量字符*第二个)
:第一(第一)、第二(第二)
{}
布尔运算符()(const std::vector和element){
返回元素[0]==第一个&元素[1]==第二个;
}
};
std::vector::const_迭代器
pos=std::find_if(words.begin()、words.end()、EqualName(“Joan”、“Smith”);
if(pos!=words.end())

std::cout@user1804029那么,忽略这个答案;它使用C++11。我假设您的编译器支持这一点,显然它不支持。如果不是键入Joan,而是键入一个包含值“Joan”的字符串变量会怎么样?当然,williams也是如此
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>

struct FindName : std::unary_function<bool, 
                                      const std::pair<std::string, std::string>>
{
    FindName( const std::pair<std::string, std::string> &p ) : p( p ){} 
    bool operator ()( const std::vector<std::string> &v ) const
    {
        return v.size() > 1 && 
               v[0] == p.first && v[1] == p.second;
    }
protected:
    const std::pair<std::string, std::string> p;
};

int main()
{
    const size_t N = 5;
    std::vector<std::vector<std::string>> v;
    v.reserve( N );

    const char * initial[N][3] =
    {
        { "Joan", "Williams", "30" },
        { "Mike", "Williams", "40" },
        { "Joan", "Smith", "30" },
        { "William", "Anderson", "20" },
        { "Sara", "Jon", "33" }
    };

    for ( size_t i = 0; i < N; i++ )
    {
        v.push_back( std::vector<std::string>( initial[i], initial[i] + 3 ) );
    }

    std::pair<std::string, std::string> p( "Joan", "Williams" );

    typedef std::vector<std::vector<std::string>>::iterator iterator;

    iterator it = std::find_if( v.begin(), v.end(), FindName( p ) );

    if ( it != v.end() )
    {
        for ( std::vector<std::string>::size_type i = 0; i < it->size(); ++i ) 
        {
            std::cout << ( *it )[i] << ' ';
        }
    }
    std::cout << std::endl;
}
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>

int main()
{
    std::vector<std::vector<std::string>> v =
    {
        { "Joan", "Williams", "30" },
        { "Mike", "Williams", "40" },
        { "Joan", "Smith", "30" },
        { "William", "Anderson", "20" },
        { "Sara", "Jon", "33" }
    };

    std::pair<std::string, std::string> p( "Joan", "Williams" );

    auto it = std::find_if( v.begin(), v.end(),
                            [&]( const std::vector<std::string> &row )
                            {
                                return row.size() > 1 &&
                                       row[0] == p.first && row[1] == p.second;
                            } );

    if ( it != v.end() )
    {
        for ( const auto &s : *it ) std::cout << s << ' ';
    }
    std::cout << std::endl;
}
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main() {
    // Requires C++11
    std::vector<std::vector<std::string>> words = {
        { "Joan", "Williams",  "30" },
        { "Mike", "Williams", "40" },
        { "Joan", "Smith", "30" },
        { "William", "Anderson", "20" },
        { "Sara", "Jon", "33" },
    };

    // Below does not require C++11
    struct EqualName
    {
        const char* first;
        const char* second;
        EqualName(const char* first, const char* second)
        :   first(first), second(second)
        {}

        bool operator () (const std::vector<std::string>& element) {
            return element[0] == first && element[1] == second;
        }
    };

    std::vector<std::vector<std::string>>::const_iterator
    pos = std::find_if(words.begin(), words.end(), EqualName("Joan", "Smith"));
    if(pos != words.end())
        std::cout << (*pos)[0] << ' ' << (*pos)[1] << ' ' << (*pos)[2] << '\n';
}