CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2009
    Posts
    4

    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].

  2. #2
    Join Date
    Aug 2009
    Posts
    4

    Re: Vectors and printing to a file

    Also, how would I print the result "v" to a text file?

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vectors and printing to a file

    Quote Originally Posted by apmca View Post
    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

  4. #4
    Join Date
    Aug 2009
    Posts
    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).

  5. #5
    Join Date
    Apr 2008
    Posts
    725

    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.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  7. #7
    Join Date
    Aug 2007
    Posts
    858

    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...

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vectors and printing to a file

    Quote Originally Posted by apmca View Post
    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

  9. #9
    Join Date
    Aug 2009
    Posts
    4

    Re: Vectors and printing to a file

    Quote Originally Posted by Paul McKenzie View Post
    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.

  10. #10
    Join Date
    Apr 2008
    Posts
    725

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured