
August 15th, 2009, 04:37 PM
#1
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].

August 15th, 2009, 04:40 PM
#2
Re: Vectors and printing to a file
Also, how would I print the result "v" to a text file?

August 15th, 2009, 04:57 PM
#3
Re: Vectors and printing to a file
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

August 15th, 2009, 05:23 PM
#4
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).

August 15th, 2009, 05:44 PM
#5
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.

August 15th, 2009, 05:52 PM
#6
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.

August 15th, 2009, 06:00 PM
#7
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...

August 15th, 2009, 06:45 PM
#8
Re: Vectors and printing to a file
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

August 15th, 2009, 07:07 PM
#9
Re: Vectors and printing to a file
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?
Last edited by apmca; August 15th, 2009 at 07:15 PM.

August 16th, 2009, 07:43 AM
#10
Re: Vectors and printing to a file
iterate over all the elements of the vector, and output the dereferenced iterator.
pseudo:
Code:
std::vector<int> vec;
// fill up the vector...
std::vector<int>::iterator iter;
loop
cout << *iter;
end loop
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
OnDemand Webinars (sponsored)
