Question:
how do you create a vector of strings in c++?
led
2011-04-11 17:58:22 UTC
Hello. I am trying to create a vector with 4 strings in them. The first string is "apple", second being "banana", third being "orange" and the fourth being "strawberry". I tried the following command:

vector fruit("apple", "banana", "orange", "strawberry") ; but this did not work.

I am using this for the private part of a class I am creating. Please help

TY
Six answers:
oops
2011-04-11 18:09:57 UTC
You can either initialize it with an array:



    string arr[] = { "apple", "banana", "orange", "strawberry" };

    vector fruit(arr, arr+4);



Or you can use push_back 4 times.



    vector fruit;

    fruit.push_back("apple");

    fruit.push_back("banana");

    fruit.push_back("orange");

    fruit.push_back("strawberry");



A new feature in the upcoming revision to the C++ standard will allow you to initialize the vector in the same way as an array, like this:



    vector fruit = { "apple", "banana", "orange", "strawberry" };



But this feature is not yet widely supported, try it on your compiler and see. I believe the latest version of GCC supports it.
?
2016-10-18 12:31:27 UTC
Vector String
?
2015-08-14 18:35:13 UTC
This Site Might Help You.



RE:

how do you create a vector of strings in c++?

Hello. I am trying to create a vector with 4 strings in them. The first string is "apple", second being "banana", third being "orange" and the fourth being "strawberry". I tried the following command:



vector<string> fruit("apple",...
iskyfire
2011-04-11 18:01:37 UTC
You can use this code as a start. Make sure to include the right header files.



#include

#include

#include

using namespace std;



#include



int main() {

keepr dontstop;

vector list;



string word;

ifstream wordfile("dic.dat");

while (infile >> word) {

list.push_back(word);

}



for (unsigned n=0; n
cout << list.at( n ) << " ";

}

return 0;

}
slafond
2011-04-11 18:11:50 UTC
Not sure of the answer, still searching but you may want to try some of the forums out there. I do know that in C++ I have not seen the complete word - string - referenced. char, strcpy, str, etc. but not the actual word string. Are you doing this from a text book?
anonymous
2016-03-16 07:24:23 UTC
What do you really want? I think your statements and code is too ambiguous. Anyhow, add a switch statement such that if the character is vowel dont add to s.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...