Vectors and printing to a file
How might I go about doing the following in C++?:
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].
Re: Vectors and printing to a file
Also, how would I print the result "v" to a text file?
Re: Vectors and printing to a file
Quote:
Originally Posted by
apmca
How might I go about doing the following in C++?:
The term "vector" has a special meaning in C++. Are you talking about the container std::vector<T>, or something else?
Regards,
Paul McKenzie
Re: Vectors and printing to a file
Sorry, I'm new to C++ :)
I just need something that will act like a (mathematical) vector (say, the C++ analogue of a vector in MATLAB or Mathematica).
Re: Vectors and printing to a file
http://www.cplusplus.com/reference/stl/vector/insert/ :)
if you want to print to file you might find fstream useful.
Re: Vectors and printing to a file
For the above task, std::vector::insert() is sufficient as stated.
However, for anything more mathematical, you should consider using Boost::uBLAS's vector class.
Re: Vectors and printing to a file
Are you talking about a mathematical vector as in this? Or a vector as some container holding objects?
I ask because while you said you're talking about a math vector, your concatenation example in the OP looks more like you want a container...
Re: Vectors and printing to a file
Quote:
Originally Posted by
apmca
How might I go about doing the following in C++?:
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
}
Regards,
Paul McKenzie
Re: Vectors and printing to a file
Quote:
Originally Posted by
Paul McKenzie
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?
Re: Vectors and printing to a file
iterate over all the elements of the vector, and output the de-referenced iterator.
pseudo:
Code:
std::vector<int> vec;
// fill up the vector...
std::vector<int>::iterator iter;
loop
cout << *iter;
end loop