The response of @HernanL . it may work, but it is not recommended because, among other things, it uses VLA , something that is not supported by the standard.
To read the strings I would bet, if you have room to maneuver, to make use of the STL, which is for something:
std::vector<std::string> LeerLinea()
{
std::string linea;
std::getline(std::cin,linea);
std::vector<std::string> toReturn;
std::istringstream iss(linea);
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(toReturn));
return toReturn;
}
What this function does is basically read a full line of text and store it in linea
, then store the string in a special stream ( istringstream
) and not , it can not be done directly.
The grace of storing the line read in this stream is that it will allow us to use the function copy
to iterate over each word. back_inserter
is a utility function that, whenever invoked, calls the push_back
method of toReturn
. In short ... each word is taken and added to the vector toReturn
.
With this we can easily read the two lines of entry:
std::cout<<"Ingrese los elementos de x: ";
std::vector<std::string> x = LeerLinea();
std::cout<<"Ingrese los elementos de y: ";
std::vector<std::string> y = LeerLinea();
And we only need to calculate the intersection. Coincidentally, in the library algorithm
there is a function that calculates the intersection between two collections of elements ... look at your things, then the easiest way to end the program could be to use this function:
std::vector<std::string> interseccion;
std::set_intersection(std::begin(x),std::end(x),
std::begin(y),std::end(y),
std::back_inserter(interseccion));
What set_intersection
does is fill in the vector interseccion
... it does not have much mystery. We can verify that the result is correct by simply iterating over the vector iteracion
:
std::cout << "Interseccion:\n";
for( std::string palabra : interseccion )
std::cout << " " << palabra << '\n';