Question
C++ #include #include #include #include Implement each of the following methods and functions. Be sure to use const, public, and private in useful and sensible
C++
#include#include #include #include
Implement each of the following methods and functions. Be sure to use const, public, and private in useful and sensible ways.
-
A default constructor str_vec() that creates a new str_vec of size 0 and capacity 10. It can be used like this:
str_vec arr; // arr is an empty str_vec of size 0 and capacity 10
-
A constructor str_vec(n s) that creates a new str_vec of size n where each element contains a copy of the string s. It can be used like this:
str_vec arr(5, "cat"); // arr is str_vec of size 5, each entry contain the string "cat"
The capacity of arr should be 5 or more.
If n is less than 0, use cmpt::error to throw a helpful error message.
-
A copy constructor str_vec(other) that creates a new str_vec that is a copy of the str_vec named other. It can be used like this:
str_vec a(5, "
"); str_vec arr(a); // arr is str_vec of size 5 with each entry equal to the string " " The capacity of arr should be at least the size of the a.
-
A destructor ~str_vec() that deletes all the dynamic memory used by this str_vec.
Be sure to test your program with valgrind!
-
Write the following three getters:
- size() returns the size of the str_vec
- capacity() returns the capacity of the str_vec
- pct_used() returns the % of the underlying array that is in use, i.e. the size divided by the capacity
Make sure that these are all const methods!
They can be used like this:
str_vec empty; cout << empty.size() << ' ' // 0 << empty.capacity() << ' ' // 10 << empty.pct_used() << ' '; // 0
-
Write the following three non-mutating methods:
- to_str() returns a string representation of this str_vec; please use the []-brackets style as shown in the example below.
- print() is a void method that prints the string representation of this str_vec to cout
- println() is a void method that does the same thing as print(), but also prints a at the end
Make sure that these are all const!
They can be used like this:
str_vec arr(3, "cat"); string s = arr.to_str(); // s is the string ["cat", "cat", "cat"] arr.print(); // prints ["cat", "cat", "cat"] on cout (no at end) arr.println(); // prints ["cat", "cat", "cat"] on cout ( at end)
-
Write the following two methods:
- get(i) is a getter that returns the string at index location of i of the str_vec. Just as with arrays, the first index location is 0. Make sure this is const!
- set(i, s) is a setter that sets index location i to be a copy of the string s
For both of these methods, use cmpt::error to throw a helpful error message if the passed-in index i is less than 0, or greater than size()-1.
They can be used like this:
str_vec arr(3, "cat"); arr.set(1, "dog"); // arr is now ["cat", "dog", "cat"] string s1 = arr.get(1); // s1 is "dog" string s2 = arr.get(2); // s2 is "cat"
-
Write the following two mutating methods:
- append(s) is a void method that adds the string s to the right end (the back) of this str_vec, increasing the size by 1
- prepend(s) is a void method that adds the string s to the left end (the front) of this str_vec, increasing the size by 1
For both of these methods, the underlying capacity should only be increased if necessary. If the capacity is increased, it should be doubled.
They can be used like this:
str_vec arr; // empty, size 0 arr.append("apple"); // ["apple"] arr.append("orange"); // ["apple", "orange"] arr.prepend("up"); // ["up", "apple", "orange"] arr.prepend("down"); // ["down", up", "apple", "orange"]
-
Write the following two mutating methods:
- reverse() is a void method that reverses the order of the elements in this str_vec; please implement this yourself using basic C++, i.e. dont just use std::reverse
- sort() is a void method that re-arranges all the strings in this str_vec into alphabetical order; please use std::sort() to help do this
They can be used like this:
str_vec arr; arr.append("d"); arr.append("b"); arr.append("a"); arr.append("c"); // ["d", "b", "a", "c"] arr.sort(); // ["a", "b", "c", "d"] arr.reverse(); // ["d", "c", "b", "a"]
-
Write the following two mutating methods:
- clear() is a void method that removes all elements from this str_vec so its size is 0; the capacity can stay the same
- shrink_to_fit() is a void method that will, if necessary, re-size the underlying array so that the size and capacity are the same; the elements in the array are not changed in any way
They can be used like this:
str_vec arr; // {}, size 0, capacity 10 arr.append("a"); // {"a"}, size 1, capacity 10 arr.append("b"); // {"a", "b"}, size 2, capacity 10 arr.shrink_to_fit(); // {"a", "b"}, size 2, capacity 2 arr.clear(); // {}, size 0, capacity 2
-
Write the following two functions (not methods!):
- operator==(a, b) is a bool function that returns true if the str_vec a and the str_vec b have the same elements in the same order, and false otherwise
- operator!=(a, b) is a bool function that returns true if the str_vec a and the str_vec b dont have the same elements in the same order, and false otherwise
They can be used like this:
str_vec a(3, "cat"); str_vec b(3, "cat"); if (a == b) cout << "same "; // prints "same" if (a != b) cout << "different "; // prints nothing a.set(0, "feline"); if (a == b) cout << "same "; // prints nothing if (a != b) cout << "different "; // prints different
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