Question

C++ VECTORS AND ITERATORS WITH STRINGS How to store a sentence of strings into a vector container with push_back until I enter and type quit? string str; vector<int> v if (str != quit) // use push_back to store str to vector BUILD 3 LOOPS TO DISPLAY THE USER DEFINED STRING SENTENCE WITH THE CONDITIONS 1.) to print out the strings using a for loop with begin and end & deferencing iterator pointer 2.) In another loop using the C++11 auto keyword to simplify the declaration of the iterator 3.) To print the contents of the vector in reverse order using rbegin and rend.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

please find below the required code for above problem:

#include<iostream>

#include<vector>

#include<string>

using namespace std;

int main()

{

vector<string> vec;

//gettingthe user input and putting them into vector

cout<<"Enter string when done enter quit in new line\n";

while(true)

{

string str;

getline(cin,str);

if(str=="quit")

break;

else

vec.push_back(str);

}

//using defferent ieratrors printing the string

cout<<"\n\nPrinting vector string using begin iterator\n";

for(vector<string>::const_iterator i = vec.begin(); i != vec.end(); ++i) {

cout << *i << "\n";

}

cout<<"\n\nPrinting vector string using auto iterator\n";

for(auto i : vec) {

cout << i << " \n";

}

cout<<"\n\nPrinting vector string in reverse order using rbegin and rend\n";

for (vector<string>::reverse_iterator i = vec.rbegin(); i != vec.rend(); ++i ) {

cout << *i << "\n";

}

}

OUTPUT

Please do let me know if u have any doubts

Add a comment
Know the answer?
Add Answer to:
How to store a sentence of strings into a vector container with push_back until I enter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for...

    Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for push_back() function as below: void push_back(string str) { // increase vector size by one // initialize the new element with str } In addition, the standard library vector doesn't provide push_front(). Implement push_front() for your vector. Test your code with the main function below. int main() {    vector v1(3);    cout<<"v1: ";    v1.print(); // this should display -, -, -    for...

  • Here is my code to put a quote down, flip it, and reverse it. I do...

    Here is my code to put a quote down, flip it, and reverse it. I do not have much knowledge on coding. Does my code only use Variables, Console I/O, Java Program Design, Intro to IDEs, If Statements, Selection and Conditional Logic, Strings, Loops, Nesting, and Iteration? If not, could it be fixed to only use them? These are the offical steps of the assignment: - Ask a user to enter a quote - whether it's several sentences or a...

  • I need help solving this question from the practice final exam given to us to prepare...

    I need help solving this question from the practice final exam given to us to prepare for the final in C++ #include <iostream> using namespace std; /* The following is code for an OrderedCollection container and its related iterator. The container has a capacity determined by a constructor parameter. The container does not grow. Code that adds elements to the container ensures that the capacity of the container is never exceeded. An attempt to add an item to a full...

  • //include the three classes we created and //vector for storing the new data #include #include #include...

    //include the three classes we created and //vector for storing the new data #include #include #include "Division.h" #include "Artifact.h" #include "Service.h" #include "Item.h" using namespace std; int main() {//use new operator to dynamically create objects of primitive    //create three new Devisions    Division* d1 = new Division();    Division* d2 = new Division();    Division* d3 = new Division();    //create three new Artifact    Artifact* a1 = new Artifact();    Artifact* a2 = new Artifact();    Artifact* a3...

  • (1) Prompt the user to enter a string of their choosing. Store the text in a...

    (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! You entered: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews...

  • Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car...

    Need help doing program In bold is problem E2 #include<iostream> #include<string> #include<vector> using namespace std; //Car class //Defined enum here enum Kind{business,maintenance,other,box,tank,flat,otherFreight,chair,seater,otherPassenger}; //Defined array of strings string KIND_ARRAY[]={"business","maintenance","other,box","tank,flat","otherFreight","chair","seater","otherPassenger"}; class Car { private: string reportingMark; int carNumber; Kind kind; //changed to Kind bool loaded; string destination; public: //Defined setKind function Kind setKind(string knd) { for(int i=0;i<8;i++) //repeat upto 8 kinds { if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array kind=(Kind)i; //setup that kind } return kind; } //Default constructor Car() { } //Parameterized constructor...

  • Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such...

    Lab 6 Instructions Objectives: • Executing and experimenting with programs that handle array related topics such as declaration, initialization, storing, retrieving, and processing elements. • Effectively manipulating and using ArrayList objects. • Becoming familiar with the use of arrays as method arguments and how array elements are passed to and returned from the called method. • Mastering the use of various debugging tools available on the Eclipse IDU. Important: EVERY one of the following Activities MUST be in a separate...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT