Define vectors "v" and "w" (doesn't really matter what they are) and then redefine "v" as the concatenation of "v" and "w".
For example, if I have v = [1,2,3] and w = [4,5,6], I want to redefine v = [v,w] = [1,2,3,4,5,6].
What if v = [1,2,3] and w = [3,4]? Would combining the two vectors be [1,2,3,4]?
If so, then use a container such as std::vector and then the std::set_union() algorithm function to combine the elements using "union" as the set operation.
Here is a small example using a vector of strings. Change this to the data type you want to use (in your case, probably int).
Code:
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>
int main()
{
std::vector<string> v(3);
std::vector<string> w(2);
std::vector<string> result;
v[0] = "Joe";
v[1] = "John";
v[2] = "Susan";
w[0] = "Susan";
w[1] = "Jack";
std::set_union(v.begin(), v.end(), w.begin(), w.end(), std::back_inserter(result));
// now result is a vector that has ["Joe", "John", "Susan", "Jack"] as elements.
v = result; // set v equal to the result
}
What if v = [1,2,3] and w = [3,4]? Would combining the two vectors be [1,2,3,4]?
If so, then use a container such as std::vector and then the std::set_union() algorithm function to combine the elements using "union" as the set operation.
Here is a small example using a vector of strings. Change this to the data type you want to use (in your case, probably int).
Code:
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>
int main()
{
std::vector<string> v(3);
std::vector<string> w(2);
std::vector<string> result;
v[0] = "Joe";
v[1] = "John";
v[2] = "Susan";
w[0] = "Susan";
w[1] = "Jack";
std::set_union(v.begin(), v.end(), w.begin(), w.end(), std::back_inserter(result));
// now result is a vector that has ["Joe", "John", "Susan", "Jack"] as elements.
v = result; // set v equal to the result
}
Regards,
Paul McKenzie
Thanks for the reply. In the case that v = [1,2,3] and w = [3,4], I would actually be looking for v = [1,2,3,3,4] instead of just the union.
Additionally, when I type cout << v at the end I am getting an error. How do you display the vector that's saved?
Last edited by apmca; August 15th, 2009 at 07:15 PM.
Bookmarks