Implement the following three c++ functions 1) no any other headers are allowed
ID: 3735985 • Letter: I
Question
Implement the following three c++ functions
1) no any other headers are allowed
2) insert() should take a pair (such as two integers) push back the second integer (value) into the sequence identified by the first integer (key).
3) data() should take one integer (key) return a pointer to a sequence of all integers inserted with that key. If no element had been inserted with given key it should return nullptr. If a sequence exists for the key, the method should return pointer to the continuous block with that sequence. Elements in the sequence must be in the same order in which they had been inserted.
4) Method size() is another access method. Its purpose is to take one integer (which is a key), and return the total number of elements inserted with that key. If no element had been inserted with given key the method should return 0.
5) here's what the code suppose to do: (example)
key_value_sequences A;
A.insert(0, 2);
A.insert(-1, 3);
A.insert(3, 2);
A.insert(0, 5);
A.insert(3, 7);
A.insert(3, 5);
A.insert(-1, 3);
A.insert(-1, 0);
auto p = A.data(3);
for (int i = 0; i < A.size(3); ++i) std::cout << p[i] << " ";
when compiled with properly implemented key_value_sequences should produce: 2 7 5.
*first integet is key, second is value*
Please only post your solution if meets the following requirements:
1) no memory leaks
2) code must be efficient. Expect millions of pairs intersted and the code should be able to handle in seconds.
3) run the code using the provided test file below and see 'pass' as ouput.
//--------------------------------implement the following----------------------------
#include <algorithm>
#include <list>
#include <vector>
class key_value_sequences {
public:
// YOU SHOULD USE C++ CONTAINERS TO AVOID RAW POINTERS
// IF YOU DECIDE TO USE POINTERS, MAKE SURE THAT YOU MANAGE MEMORY PROPERLY
// IMPLEMENT ME: SHOULD RETURN SIZE OF A SEQUENCE FOR GIVEN KEY
// IF NO SEQUENCE EXISTS FOR A GIVEN KEY RETURN 0
int size(int key) const;
// IMPLEMENT ME: SHOULD RETURN POINTER TO A SEQUENCE FOR GIVEN KEY
// IF NO SEQUENCE EXISTS FOR A GIVEN KEY RETURN nullptr
const int* data(int key) const;
// IMPLEMENT ME: INSERT VALUE INTO A SEQUENCE IDENTIFIED BY GIVEN KEY
void insert(int key, int value);
}; // class key_value_sequences
/--------------------------------------use the following code to test-----------------------
#include <iostream>
#include "a3.hpp"
int main(int argc, char* argv[]) {
key_value_sequences A;
{
key_value_sequences T;
// k will be our key
for (int k = 0; k < 10; ++k) {
// v is our value
// here we are creating 10 sequences:
// key = 0, sequence = (0)
// key = 1, sequence = (0 1)
// key = 2, sequence = (0 1 2)
// ...
// key = 9, sequence = (0 1 2 3 4 5 6 7 8 9)
for (int v = 0; v < k + 1; ++v) T.insert(k, v);
}
T = T;
key_value_sequences V = T;
A = V;
}
std::vector<int> ref;
if (A.size(-1) != 0) {
std::cout << "fail" << std::endl;
return -1;
}
for (int k = 0; k < 10; ++k) {
if (A.size(k) != k + 1) {
std::cout << "fail";
return -1;
} else {
ref.clear();
for (int v = 0; v < k + 1; ++v) ref.push_back(v);
if (!std::equal(ref.begin(), ref.end(), A.data(k))) {
std::cout << "fail";
return -1;
}
}
}
std::cout << "pass" << std::endl;
return 0;
} // main
Explanation / Answer
here is your program : ----------->>>>>>>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
class key_value_sequences {
vector<list<int>> datas;
vector<int> key;
public:/*
// YOU SHOULD USE C++ CONTAINERS TO AVOID RAW POINTERS
// IF YOU DECIDE TO USE POINTERS, MAKE SURE THAT YOU MANAGE MEMORY PROPERLY
// IMPLEMENT ME: SHOULD RETURN SIZE OF A SEQUENCE FOR GIVEN KEY
// IF NO SEQUENCE EXISTS FOR A GIVEN KEY RETURN 0
*/int size(int key) const;/*
// IMPLEMENT ME: SHOULD RETURN POINTER TO A SEQUENCE FOR GIVEN KEY
// IF NO SEQUENCE EXISTS FOR A GIVEN KEY RETURN nullptr
*/const int* data(int key) const;/*
// IMPLEMENT ME: INSERT VALUE INTO A SEQUENCE IDENTIFIED BY GIVEN KEY
*/void insert(int key, int value);
};
int key_value_sequences::size(int key)const{
for(int i = 0;i<this->key.size();i++){
if(key == this->key[i]){
return datas[i].size();
}
}
return 0;
}
void key_value_sequences::insert(int key,int value){
for(int i = 0;i<this->key.size();i++){
if(key == this->key[i]){
datas[i].push_back(value);
return;
}
}
this->key.push_back(key);
list<int> temp;
temp.push_back(value);
datas.push_back(temp);
}
const int* key_value_sequences::data(int key)const{
int s = 0;
int i;
for(i = 0;i<this->key.size();i++){
if(key == this->key[i]){
s = datas[i].size();
break;
}
}
if(s == 0){
return NULL;
}
int *p = new int[s];
int j = 0;
for(auto const& it : datas[i]){
p[j] = it;
j++;
}
return p;
}