Please can someone use vectors in a program and explain what the code does. I came here at an earlier time to get an explanation to it but didn't really understand it. I've tried to figure it out myself and have to an extent.
Could you write a program that uses vectors, strings and/or ints, maybe char (no classes or arrays).
Thanks.
You didn't specify what type of vector it is. What is the second for loop for and why do you output vec[i] and not vec (i know you can't just output vec because it gives some kind of error but why).
This is a pretty simple example, and I'm still learning the complexities of vectors myself, but unlike you I have previous experience with arrays. I'm sure someone else could demonstrate the greater functionality of vectors than I can, as I tend to use them more like arrays. At any rate, this should give you some idea of how it all works.
#include <vector>
usingnamespace std;
int main()
{
int x=0,input=0,sum=0;
cout<<"How many values would you like to sum? ";
cin>>x;
//declares vector of x ints initialized to 0
vector<int> vec(x,0);
//prompts user for values to fill the vector
for (int i=0; i<x; i++)
{
cout<<"Please enter an integer value: ";
cin>>input;
vec[i]=input;
}
//tallies a running total before output
for (int i=0; i<x; i++)
sum+=vec[i];
//outputs total of all values
cout<<"The sum total was "<<sum<<".\n";
//output all vector values
cout<<"Your vector contains the following values:\n";
for (int i=0; i<x; i++)
cout<<vec[i]<<" ";
cout<<endl<<endl;
return 0;
}
For your particular application, arrays are better (you know the size before starting). Vectors are good when you don't know the max size, and when you want to insert objects in the middle of the vector.
int main()
{
std::cout << "enter 10 numbers, I'll add them: ";
int arr[10];
for (int i = 0; i < 10; ++i)
{
std::cin >> arr[i];
}
int total = 0;
for (int i = 0; i < 10; ++i)
total += arr[i]; // this could work there too -->
std::cout << "total: " << total;
return 0;
}
int main()
{
std::cout << "enter 10 numbers, I'll add them: ";
std::vector<int> arr;
for (int i = 0; i < 10; ++i)
{
int temp;
cin >> temp;
arr.push_back(temp);
}
int total = 0;
for (std::vetor<int>::iterator it = arr.begin(); it != arr.end(); ++it)
total += *it; // alternate method to iterate
std::cout << "total: " << total;
return 0;
}
That's actually quite interesting, Stewbond. As I'm still getting familiar with vectors myself, can you explain how your for loop on line 14 (the one that uses ::iterator) works? Are my above for loops more applicable to arrays, or is it merely a matter of programmer preference?
What if a vector is created and then the program asks the user to input 10 numbers then asks him how many of those 10 numbers he wants to sum. How would that be done?
I'm sure it can be done, but how would you specify which numbers get summed? If it were merely to take the 10 numbers, and the user specifies that 3 values get summed, should it just sum the first three values? That should be simple enough to implement. If you wish to specify which values get summed, the program could increase in complexity, though. It really depends on what you want it to do.
int main()
{
std::cout << "Enter numbers, -1 to exit: ";
int arr[1000]; // Don't know max size, choose something big
int size = 0; // track actual size in parallel label
while (true)
{
int temp;
cin >> temp;
if (temp == -1) break;
arr[size++] = temp;
}
int total = 0;
for (int i = 0; i < size; ++i)
total += arr[i];
std::cout << "total: " << total;
return 0;
}
int main()
{
std::cout << "Enter numbers, -1 to exit: ";
std::vector<int> arr; // handles any size
// size not needed (but available in arr.size()).
while (true)
{
int temp;
cin >> temp;
if (temp == -1) break;
arr.push_back(temp);
}
int total = 0;
for (int i = 0; i < arr.size(); ++i)
total += arr[i]; // same as iterator example above.
std::cout << "total: " << total;
return 0;
}
With std::vetor<T>::iterator, we don't need to know the size of the array. we just go until we reach the end. An iterator is effectively a pointer to an element in the vector. The ++ operator makes the iterator point to the next element in the set. That's all there is to is!
#include <vector>
usingnamespace std;
int main()
{
int x=0,input=0,sum=0;
cout<<"How many values would you like to sum? ";
cin>>x;
//declares vector of ints (quantity unknown)
vector<int> vec;
//prompts user for 10 values to fill the vector
for (int i=0; i<10; i++)
{
cout<<"Please enter an integer value: ";
cin>>input;
vec.push_back(input);
}
//tallies a running total of only the desired quantity of values before output
for (int i=0; i<x; i++)
sum+=vec[i];
//outputs total of summed values
cout<<"The sum total of your first "<<x<<" values was "<<sum<<".\n";
//output all vector values
cout<<"Your vector contains the following values:\n";
for (int i=0; i<10; i++)
cout<<vec[i]<<" ";
cout<<endl<<endl;
return 0;
}
How many values would you like to sum? 3
Please enter an integer value: 1
Please enter an integer value: 2
Please enter an integer value: 5
Please enter an integer value: 7
Please enter an integer value: 9
Please enter an integer value: 2
Please enter an integer value: 4
Please enter an integer value: 6
Please enter an integer value: 8
Please enter an integer value: 0
The sum total of your first 3 values was 8.
Your vector contains the following values:
1 2 5 7 9 2 4 6 8 0
I gained a greater understanding of the use of push_back by participating in this thread, but I still don't understand the iterator based loop. Apparently regardless of the method used for the loop, the total is incremented by the vector position's value, so I suppose it's not terrible important.
EDIT: With Stewbond's comments on the iterator based loop and a couple edits, I now understand how it works (Thanks Stewbond!). In this particular case, either method will work. When you attempt to tackle arrays later, Nathan, remember they work similarly, but you have to know the number of values involved when creating it, unlike with vectors, and you cannot add more elements to an array the way more elements can be added to a vector using push_back.
The ash background is the one that uses vectors. It demonstrates the same activities while using arrays versus vectors.
int arr[10]; is similar to writing std::vector<int> arr; with the exception that you don't need to declare a size.
arr[size++] = temp; is the same as arr.push_back(temp); with the exception that you don't need to track the size yourself (it's done internally).
for (int i = 0; i < size; ++i) is similar to for (int i = 0; i < arr.size(); ++i) with the exception that the size was automatically calculated for you within arr.
While I didn't make use of it (still familiarizing myself with vectors), he was pointing out that arr.size() can be used to get the size of the vector (at least when the vector is named arr), and he based his loop on that. I went the easier way, knowing that you had specified 10 values, but there may be times that the value is unknown, so vectorname.size() can come in handy, and a loop based on it will work regardless of the vector size, whereas my code would require modification of the values in my loops if the vector size changed.
I found that last link helpful too, and was able to construct a simple example of vector alteration that is not dependent upon its size so Nathan can see what we mean here.
#include <vector>
usingnamespace std;
int main()
{
int SIZE = 10;
//declares vector of integers (quantity unknown)
vector<int> vec;
//loop populates vector with values beginning from 1
for (int i=0;i<SIZE;i++)
vec.push_back(i+1);
//adds 2 11's to the end of vector
vec.insert (vec.end(),2,11);
//removes last entry in vector
vec.pop_back();
//adds a 0 to end of vector
vec.push_back(0);
//outputs whole vector
for (int i=0;i<vec.size();i++)
cout<<vec[i]<<" ";
return 0;
}
Go ahead and play with the value of SIZE, you will see that regardless of the value, the whole vector is outputted in the last loop. This means that a vector could be based on user input, or a running tally, or whatever we want to do. I find the syntax I use a bit more comprehensible than some of the examples I found, but they work similarly I believe. In a nutshell, it means we don't need to know the size of the vector to perform various operations; outputting it is just a simple example.