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

时间:2014-11-30 15:32:37

标签: c++ vector

考虑以下向量

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

这是一个三维向量;

现在考虑以下要素:

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

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

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

4 个答案:

答案 0 :(得分:1)

我强烈建议您使用带有重载等于运算符而不是vector<string>的数据结构(特别是因为看起来第三个元素应该保存在整数而不是字符串中)。

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

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

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

答案 1 :(得分:1)

以下是两个针对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;
}

这两个程序的putput是

琼·威廉姆斯30

答案 2 :(得分:0)

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

for(auto r: a_words)
    if(r[0] == "Joan" && r[1] == "Williams")
        cout << r[0] << " " << r[1] << " " << r[2] << endl;

答案 3 :(得分:0)

基本上@Columbo的答案很好,消除了C ++ 11的功能(除了初始化):

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