i got difficulty here, i dont know how to create a random vector consist of 4 strings. using vector.

``````int main ()
{
string red,yellow,blue,green;
}``````

-im a beginner for c++

## All 5 Replies

Use std::vector<std::string> Now, your question could be interpreted in a few ways. The ambiguity probably comes from a language difference.

A random vector for those four colours is best achieved via a shuffle algorithm.
I assume you want to return a vector with all four colours except in a different order.

If you're unfamiliar with the `std::shuffle function` ... don't quote me on that just - it's been a while... You could achieve something similar by randomly picking two elements and swapping their contents perhaps?

If your problem corresponds to iamthwee's interpretation, then his solution is appropriate.

If your problem is that you want to create a vector of arbitrary size where each element is one of the four color-strings, picked at random. Then, a simple solution is to map each color to a number and generate the numbers randomly (e.g. with rand() for a quick and easy solution). As so:

``````#include <map>
#include <string>
#include <ctime>
#include <cstdlib>
#include <iostream>

using namespace std;

int main() {
map< int, string> words;
words = "red";
words = "blue";
words = "green";
words = "yellow";

srand((unsigned int)time(NULL));
vector<string> v(10); // create a vector of 10 elements.
for(vector<string>::iterator it = v.begin(); it != v.end(); ++it) {
*it = words[rand() % 4];
cout << *it << endl;
};
return 0;
};``````

guys,

i tried this code

``````#include <iostream>
#include <vector>
#include <string>
#include<ctime>
#include<cstdlib>
#include <map>

using namespace std;

int main ()
{

vector<string> words(4);
vector<string>::iterator it;
words.push_back("red");
words.push_back("yellow");
words.push_back("blue");
words.push_back("green");
for(vector<string>::iterator it=words.begin();it !=words.end();++it)
{
srand((unsigned int)time(NULL));
*it=words[rand()%words.size()];
cout<<*it<<endl;
}
return 0;

}``````

the output not as i expected. it just output the same colour..

i expect the output like this:
"red,yellow,green,blue" (those four colors are randomly shuffle for every attempt) :'( help me with this code. i want to build mastermind code.

You want to use random_shuffle function,

``````#include <algorithm>
#include <vector>
#include <string>
#include <iostream>

int main(){
using namespace std;
const int COLOR_SIZE = 4;
string colors[COLOR_SIZE ] = { "yellow", "green", "blue" , "red" }
std::vector<string> colorVec( colors, colors + COLOR_SIZE  );
//shuffle colors
std::random_shuffle( colorVec.begin(), colorVec.end() );

//do stuff with colorVec like print it
for(int i = 0; i < colorVec.size(); ++i){
cout << colorVec[i] << endl;
}
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.