Tuesday 12 February 2019

c++ - How do I insert two values into a or so that I can retrieve values later on?

Answer


Answer





Code worked on:



#include 
#include
#include

class key_value_sequences {
public:


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

private:
list< pair > > myList;

}; // class key_value_sequences

#endif


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

v.push_back(value);
myList.push_back(make_pair(key, v));
return;
};

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

}
}
vector v;
return v;
};

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

return it->second.size();
}
}
return -1;
};


I am getting errors for template arguments, and can't figure out why. It looks like this line



std::list< pair > > myList;



is throwing errors



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

^


I can't figure out why.



I'm also stuck with errors



/usr/include/c++/5/bits/stl_algobase.h:840:58: error: no type named ‘value_type’ in ‘struct std::iterator_traits >’
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 >’
&& __are_same<_ValueType1, _ValueType2>::__value);
^


The instantiation of the iterator is:



list>>::iterator it;



Edit trying out vector hashtable:



   class key_value_sequences {
public:

int size(int key);


int* data(int key);



void insert(int key, int value);

private:
vector>> hash_table;
list>>::iterator it;

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

}

}; // class key_value_sequences

#endif // A3_HPP

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

list> &collisionlist = hash_table[hash(key)];



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


collisionlist.push_back(pair(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;
};

Answer



Tried adding as much detail as I could in the comments, but this successfully passed all tests on my end. While I mentioned I reduced the original copy constructor from O(keys.length + vals.size) to just O(vals.size)- I lied.



resize() is linear in the length of the vector- so it's best to leave that alone.



 #include 
#include

#include

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 *> keys;
list > 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); //resize the vector to make room

vector 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

}
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...