c++ - 在多维 vector 中搜索两个特定元素

标签 c++ vector

考虑以下 vector

vector<vector<string>> a_words(80000,vector<string>(3));

这是一个三维 vector ;

现在考虑以下元素:

  Joan Williams 30
  Mike Williams 40
  Joan Smith 30
  William Anderson 20
  Sara Jon 33

基本上我想按行搜索,我想找到 Joan Williams,记住 Joan 是第一列中的一个元素,Williams 是第二列中的一个元素

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

最佳答案

这里有两个演示程序,一个用于 C++ 2003,另一个用于 C++ 2011,用于执行搜索

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;
}

C++ 2011

#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;
}

两个程序的输出都是

琼·威廉姆斯 30 岁

关于c++ - 在多维 vector 中搜索两个特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27214890/

相关文章:

c++ - 为什么我在为 vector <int> 实现这个简单的散列时出错?

c++ - 虚拟功能 : Iterating over a vector<Base Class> that is populated with subclass objects

c++ - 返回类型为char*的成员函数在while循环后返回包含不同字符串的地址

类似 MPI 的 C++ 消息传递库

C++11 将 vector 传递给构造函数

matrix - 在 Racket 中设置多维向量的各个元素

c++ - 获取STL vector C++的地址

c++ - 对内存中同一地址的写入之间可能存在数据竞争

c++ - 从粗糙点生成三次贝塞尔曲线

Java 6 SE vector 过时了吗?