Here is the question:
(Implementing vector class) The vector class is provided in the standard C++ library. Implement the vector class an exercise. The standard vector class has many functions. For this exercise , implement only the functions defined in the UML class diagram, as shown in the image below. Use dynamic memory allocation where appropriate.
I'm really not even sure how to start this out. I understand how to use templates and vectors separately, but together and in a class we haven't stepped into yet. Any help with getting me started would be greatly appreciated!!
template <typename T>
class Vector
{
public:
Vector();
T at(size_t idx) const;
T& at(size_t idx);
//...
private:
T* _vals;
}
template <typename T>
T Vector<T>::at(size_t idx) const
{
//...
}
Of course you can start with step 2 or 3 right away if you feel save.
I'd recommend starting with step 2 since it makes debugging easier and transformation is not that hard afterwards.
Why would you do typedef...why not go right to putting the template tags?
Also I am familiar with the vector format, but is there anything special you have to do to the header and/or implementation files in order for the class to operate correctly?
Here's what I have done thus far. I know that the vector class is part of the C++ library, but this assignment wants us to manually create the member functions in the UML diagram. http://i56.tinypic.com/34f1wg4.png
It errors out in the main() anywhere I try to use "intVector." Tells me expression must have class type.
#include<iostream>
#include<iomanip>
#include "vector.h"
#include <string>
usingnamespace std;
int main()
{
Vector<int> intVector();
// Store numbers 1, 2, 3, 4, 5, ....10 to the vector
for (int i = 0; i < intVector.size(); i++)
{
intVector.push_back(i + 1);
}
// Display the numbers in the vector
cout << "Numbers in the vector: ";
for (int i = 0; i < intVector.getSize(); i++)
{
cout << intVector[i] << " ";
}
Vector<string> stringVector;
// Store strings into the vector
stringVector.push_back("Dallas");
stringVector.push_back("Houston");
stringVector.push_back("Austin");
stringVector.push_back("Norman");
// Display the string in the vector
cout << "\nStrings in the string vector: ";
for (int i = 0; i < 10; i++)
cout << stringVector[i] << " ";
stringVector.pop_back(); // Remove the last element
Vector<string> v2;
v2.swap(stringVector);
v2[0] = "Atlanta";
// Redisplay the string in the vector
cout << "\nStrings in the vector v2: ";
for (int i = 0; i < v2.getSize(); i++)
cout << v2.at(i) << " ";
return 0;
}
Do you know what that means? Vector<int> intVector();
It's a declaration of a function intVector taking no arguments and returning a Vector<int>!
replace it with Vector<int> intVector;
Did you read jsmith's comment:
Template classes must be completely implemented within the header file or within a file #included by the header file.
Do you know what that means?
Vector<int> intVector();
It's a declaration of a function intVector taking no arguments and returning a Vector<int>!
replace it with
Vector<int> intVector;
Ok, so that is for the default constructor. Now if I have arguments like below...how come it still does work?
Vector<int> intVector(10);
I don't understand why I have to define the operator..I thought vectors were supposed to work like arrays.
I don't understand why I have to define the operator..I thought vectors were supposed to work like arrays.
They do, if you make them this way. Of course, the job is already done if you use the appropriate STL container - http://www.cplusplus.com/reference/stl/vector/. But you have to do something similar yourself, if I understand correctly - reinvent the wheel type of exercise. Just as the STL version redefines the subscript operator, so should you, if you want your Vector to be used as array.
#include<iostream>
#include<iomanip>
#include "test.h"
#include <string>
usingnamespace std;
int main()
{
vector<int> intVector(10);
// Store numbers 1, 2, 3, 4, 5, ....10 to the vector
for (int i = 0; i < intVector.length(); i++)
{
intVector.push_back(i + 1);
}
// Display the numbers in the vector
cout << "Numbers in the vector: ";
for (int i = 0; i < intVector.length(); i++)
{
cout << intVector[i] << " ";
}
vector<string> stringVector;
// Store strings into the vector
stringVector.push_back("Dallas");
stringVector.push_back("Houston");
stringVector.push_back("Austin");
stringVector.push_back("Norman");
// Display the string in the vector
cout << "\nStrings in the string vector: ";
for (int i = 0; i < 10; i++)
cout << stringVector[i] << " ";
stringVector.pop_back(); // Remove the last element
vector<string> v2;
v2.swap(stringVector);
v2[0] = "Atlanta";
// Redisplay the string in the vector
cout << "\nStrings in the vector v2: ";
for (int i = 0; i < v2.length(); i++)
cout << v2.at(i) << " ";
system("pause");
return 0;
}
Onur...yes I saw the error..I put 10 just to test it out with hard numbers and forgot to change it back to stringVector.length()
Mercurialol - I'm not sure what you mean...push_back is supposed to work the way you described. I push elements and the size keeps increasing. Vectors are similar to arrays, but their size increase if they are full. Is there something wrong with the code you pasted?
Why take the index as reference? You don't want to modify it, it's inefficient, it doesn't allow you to use it with a literal (vector.at(1)), since you cannot take a non-const reference to a literal. You maybe want to change it to this:
vector::itemType& vector::at(int index);
This allows you to modify the element at position index.
Also try to think what should happen if a vector goes out of scope, i.e. is destructed?
The default destructor won't do it.