the code is listed after the question 1. When you try to get a vector element be
ID: 3628198 • Letter: T
Question
the code is listed after the question1. When you try to get a vector element beyond the end, will happen?
Try to print the 10th element; and try to assign a value to the 10th element.
Try to assign a value (‘a’) to the 15th element of an empty vector (charvect). What happens?
What happens if you try to change the back() value (to ‘b’) of charvect when the vector is empty?
What happens if you execute pop_back when the vector is empty? (Hint: run the program once with this in and once without it…what’s the difference?)
Look at the last section of commented out code. What do you think the output will be? Now uncomment the last section of code and execute. What is the actual output?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Declare variables
vector<int> intvect;
vector<char> charvect;
int temp;
/*cout << "Enter 5 integer values" << endl;
for (int i = 0; i < 5; i++)
{
cin >> temp;
intvect.push_back(temp);
}
*/
//try to access intvect beyond the end
cout << "Enter 5 integer values" << endl;
for (int i = 0; i <= 10; i++)
{
cin >> temp;
intvect.push_back(temp);
}
// print the 10th element
// now assign the value 99 to the 10th element
// print the 10th element
// assign the value 'a' to the 15th element of charvect
//notice that charvect is empty
// change the "back" value of charvect to a 'b'
// print the "back" value of charvect
// do a pop_back on charvect and see what happens
//here's the commented out section
/* vector<char> letters;
vector<char>::iterator itr;
letters.push_back('f');
letters.push_back('i');
letters.push_back('e');
letters.push_back('r');
letters.push_back('c');
letters.push_back('e');
itr = letters.begin();
cout << *itr;
itr++;
cout << *itr;
cout << letters[3];
itr += 4;
cout << *itr;
*/
cout << endl << "done" << endl;
return 0;
}
Explanation / Answer
#include < iostream >
#include < vector >
using namespace std;
int main()
{
// Declare variables
vector< int > intvect;
vector< char > charvect;
int temp;
/*cout << "Enter 5 integer values" << endl;
for (int i = 0; i < 5; i++)
{
cin >> temp;
intvect.push_back(temp);
}
*/
//try to access intvect beyond the end
cout << "Enter integer values" << endl;
for (int i = 0; i <= 10; i++)
{
cin >> temp;
intvect.push_back(temp);
}
cout<<"10th element:"<<intvect[10]<<endl;
intvect.pop_back();
intvect.push_back(99);
cout<<"10th element After assign:"<<intvect[10]<<endl;
for (int i = 0; i < =15; i++)
{
charvect.push_back('a');
}
// assign the value 'a' to the 15th element of charvect
if(charvect.empty())
cout<<"Vector Empty";
//notice that charvect is empty
// change the "back" value of charvect to a 'b'
for (int i = 0; i <=15; i++)
{
charvect.pop_back();
}
for (int i = 0; i <=15; i++)
{
charvect.push_back('b');
}
// print the "back" value of charvect
cout<<"Display:";
for (int i = 0; i <=15; i++)
{
cout<<charvect[i]<<endl;
}
// do a pop_back on charvect and see what happens
system("pause");
cout << endl << "done" << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.