Question
Implement the following three c++ functions Please DO NOT reply to this post if u saw this post before! PLEASE DONT WASTE YOUR TIME Please
Implement the following three c++ functions
Please DO NOT reply to this post if u saw this post before! PLEASE DONT WASTE YOUR TIME
Please DO NOT reply to this post if u saw this post before! PLEASE DONT WASTE YOUR TIME
Please DO NOT reply to this post if u saw this post before! PLEASE DONT WASTE YOUR TIME
Please DO NOT reply to this post if u saw this post before! PLEASE DONT WASTE YOUR TIME
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
1) no memory leaks
1) no memory leaks
1) no memory leaks
1) no memory leaks
1) no memory leaks
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 #include
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
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
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
Please DO NOT reply to this post if u saw this post before! PLEASE DONT WASTE YOUR TIME
Please DO NOT reply to this post if u saw this post before! PLEASE DONT WASTE YOUR TIME
Please DO NOT reply to this post if u saw this post before! PLEASE DONT WASTE YOUR TIME
Please DO NOT reply to this post if u saw this post before! PLEASE DONT WASTE YOUR TIME
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started