如何在<list>或<vector>中插入两个值,以便稍后可以检索值?

时间:2016-10-15 16:24:46

标签: c++ string vector insert

代码工作:

#include <algorithm>
#include <list>
#include <vector>

class key_value_sequences {
public:

    int size(int key);
    int * data(int key);
    void insert(int key, int value);

private:
    list< pair<int, vector<int> > > myList;

}; // class key_value_sequences

#endif

void key_value_sequences::insert(int key, int value){
    list< pair<int, vector<int> > >::iterator it;
    for(it = myList.begin(); it != myList.end(); ++it){
        if (it->first == key){
            it->second.push_back(value);
            return;
        }
    }
    vector<int> v;
    v.push_back(value);
    myList.push_back(make_pair(key, v));
    return;
};

int * key_value_sequences::data(int key){
    list< pair<int, vector<int> > >::iterator it;
    for(it = myList.begin(); it != myList.end(); ++it){
        if (it->first == key){
            return (it->second);
        }
    }
    vector<int> v;
    return v;
};

int key_value_sequences::size(int key){
    list< pair<int, vector<int> > >::iterator it;
    for(it = myList.begin(); it != myList.end(); ++it){
        if (it->first == key){
            return it->second.size();
        }
    }
    return -1;
};

我收到模板参数的错误,无法弄清楚原因。它看起来像这一行

std::list< pair<int, vector<int> > > myList;

正在抛出错误

 error: template argument 1 is invalid
 std::list< pair<int, vector<int> > > myList;
                                  ^
 error: template argument 2 is invalid
 error: expected unqualified-id before ‘>’ token
 std::list< pair<int, vector<int> > > myList;
                                    ^

我无法弄清楚原因。

我也遇到了错误

/usr/include/c++/5/bits/stl_algobase.h:840:58: error: no type named ‘value_type’ in ‘struct std::iterator_traits<std::vector<int> >’
   typedef typename iterator_traits<_II2>::value_type _ValueType2;
                                                      ^
/usr/include/c++/5/bits/stl_algobase.h:845:9: error: no type named ‘value_type’ in ‘struct std::iterator_traits<std::vector<int> >’
         && __are_same<_ValueType1, _ValueType2>::__value);
         ^

迭代器的实例化是:

list<pair<int, vector<int>>>::iterator it;

编辑试用矢量哈希表:

   class key_value_sequences {
public:

int size(int key);


int* data(int key);


void insert(int key, int value);

private:
    vector<list<pair<int,int>>> hash_table;
    list<pair<int, vector<int>>>::iterator it;

    int hash(int value)
    {
        return abs(value%static_cast<int>(hash_table.size()));
    }

}; // class key_value_sequences

#endif // A3_HPP

void key_value_sequences::insert(int key, int value){

          list<pair<int,int>> &collisionlist = hash_table[hash(key)];


          for (std::pair<int,int> &test: collisionlist)
          {
              if (key == test.first)
              {
                  test.second = value; // update existing
                  return;
              }
          }

          collisionlist.push_back(pair<int,int>(key, value));
};

int* key_value_sequences::data(int key){

    for(it = hash_table.begin(); it != hash_table.end(); ++it){
        if (it->first == key){
            return &(it->second[0]);
        }
    }
    return nullptr;
};

int key_value_sequences::size(int key){

    for(it = hash_table.begin(); it != hash_table.end(); ++it){
        if (it->first == key){
            return it->second.size();
        }
    }
    return -1;
};

3 个答案:

答案 0 :(得分:1)

尝试在评论中添加尽可能多的细节,但这成功地通过了我的所有测试。虽然我提到我将原始副本构造函数从O(keys.length + vals.size)缩减为O(vals.size) - 我撒了谎。

resize()vector的长度上是线性的 - 所以最好不要单独留下它。

 #include <iostream>
 #include <vector>
 #include <list>

 using namespace std;

 class key_value_sequences{
    public:
        int size(int key);
        int * data(int key);
        void insert(int key, int value);
        key_value_sequences(){};                               //ctor
        key_value_sequences(const key_value_sequences &_rhs); //[heli]coptor
        ~key_value_sequences(){};                            //dtor
    private:
        vector <vector<int> *> keys;
          list <vector<int>  > vals;
};

key_value_sequences::key_value_sequences(const key_value_sequences &_rhs){
    keys.resize(_rhs.keys.size());         //resize new kvs key vector to old size
    auto it = _rhs.vals.begin();

    while (it != _rhs.vals.end()){
        vals.push_back(*it);            //push back value vector to list
        keys[(*it)[0]] = &vals.back(); //use the prepended key value of value vector
        ++it;                         //            to reestablish ref in key vector
    }
}

void key_value_sequences::insert(int key, int value){
    if (key > -1 && key + 1 > keys.size()){    //if key index is valid & > key vector size
        keys.resize(key+1, new vector<int>);  //resize the vector to make room

        vector<int> v;
        vals.push_back(v);                 //push back new value vector to list
        vals.back().push_back(key);       //create key @ front of list for the [heli]coptor
        vals.back().push_back(value);    //push back initial value
        keys[key] = &vals.back();       //update reference in key vector
    }
    else if (key > -1){
        keys[key]->push_back(value); //index already exists, push back value to value vector
    }

    return;
}

int * key_value_sequences::data(int key){
    if (key + 1 > keys.size() || key < 0){
        return nullptr;
    }
    else{
        return &keys[key]->at(1);   //if index is valid: return second element of value vector
    }                              //in order to account for the prepended key
}

int key_value_sequences::size(int key){
    if (key < 0 || keys[key]->empty() || key + 1 > keys.size()){
        return -1;
    }
    else{
        return keys[key]->size() - 1; //if index is valid: return size - 1 to account for key
    }
}

答案 1 :(得分:0)

要回答问题的标题,您可以使用push_backstd::list的{​​{1}}方法将项​​目放入这些容器中。

项目将保留在容器中,直到删除容器,删除项目或程序停止执行。

对于容器中的std::vector项,您可以使用循环进行搜索。 findstd::list都支持迭代器,以便迭代通过容器。可以使用数组语法检索std::vector中的项目。

答案 2 :(得分:0)

听起来我需要一个多图。映射是一个容器,允许您插入键/值对,其中键可用于查找值。多图可以让您拥有与单个键关联的多个值。

例如:

    std::multimap<int, int> myMap;

    myMap.insert( std::make_pair( 0, 8 ) );
    myMap.insert( std::make_pair( 0, 5 ) );
    myMap.insert( std::make_pair( 0, 7 ) );
    myMap.insert( std::make_pair( 1, 15 ) );

    // equal_range() returns a pair of iterators pointing to the first item
    // in the list for the specified key, and one past the final item containing
    // the key.
    auto searchResultIteratorPair = myMap.equal_range( 0 );

    // Print out the values found
    for( auto it = searchResultIteratorPair.first; it != searchResultIteratorPair.second; it++ )
    {
        std::cout << "Value: " << it->second << std::endl;
    }

如果我的假设是错误的并且您确实想要使用列表/向量,那么您需要将它们创建为对的列表/向量。然后,要查找项目,您将迭代整个列表并检查每一对,看它是否符合您的标准。

例如:

std::list< std::pair<int, int> > myList;

    myList.push_back( std::make_pair( 0, 8 ) );
    myList.push_back( std::make_pair( 0, 5 ) );
    myList.push_back( std::make_pair( 0, 7 ) );
    myList.push_back( std::make_pair( 1, 15 ) );

    int searchValue = 0;
    for( auto it = myList.begin(); it != myList.end(); it++ )
    {
        if( it->first != searchValue )
            continue;

        std::cout << "Value: " << it->second << std::endl;
    }
相关问题