Vectors and Unions

I am unsure as to why my set_union isn't working. Possibly something simple? Any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <stdlib.h>

using namespace std;

const int Error = 1; // return(Error);

//-----------------------------------------------------------------------------

int main(int argc, char *argv[])
{
 if(argc != 3)	// Usage statement
  {
   cout << "\nUsage: " << argv[0] << " 'File Name 1'";
   cout << " 'File Name 2'" << endl;
   return(Error);
  }

 ifstream input_File1(argv[1], ios::in); // Open input file

 if(input_File1.is_open() && input_File1.good())
  {
   cout << "\n" << argv[1] <<" is open..." << endl << endl;
  } 
 else
  {
   cout << "\nInput File 1 did NOT open..." << endl;
   return (Error);
  }

//-----------------------------------------------------------------------------

 ifstream input_File2(argv[2], ios::in); // Open input file

 if(input_File2.is_open() && input_File2.good())
  {
   cout << "\n" << argv[2] <<" is open..." << endl << endl;
  } 
 else
  {
   cout << "\nInput File 2 did NOT open..." << endl;
   return (Error);
  }

//-----------------------------------------------------------------------------

 string line;          // Local Variables
 double num=0, sum=0;

 vector<double> info1; // Vectors for file input
 vector<double> info2;
 vector<double> info3;
 vector<double>::iterator it;

 cout << " Items within File 1" << endl;
 cout << "---------------------" << endl;

 while(getline(input_File1, line)) // Reads strings from file
  {
   istringstream(line) >> num;  // Converts numbers from string to double
   info1.push_back(num);        // Inserts strings into array
  }
 sort(info1.begin(), info1.end()); // Sort vector 1
   
   for(int i=0; i<info1.size()-1; i++) // Printing out list til second to last
    {
     cout << info1[i] << ", "; // Output
    }
   cout << info1.back() << "." << endl; // Prints last item for proper format
   cout << "\nSize of Array 1: "  << info1.size() << endl << endl;
  
//----------------------------------------------------------------------------- 
  
   cout << "\n Items within File 2" << endl;
   cout << "---------------------" << endl;

   while(getline(input_File2, line)) // Reads strings from file
    {
     istringstream(line) >> num; // Converts numbers from string to integer
     info2.push_back(num);        // Inserts numbers into array
    }
   sort(info2.begin(), info2.end()); // Sort vector 2
   
   for(int i=0; i<info2.size()-1; i++) // Printing out list til second to last
    {
     cout << info2[i] << ", "; // Output
    }
   cout << info2.back() << "." << endl; // Prints last item for proper format
   cout << "\nSize of Array 2: "  << info2.size() << endl << endl;

//-----------------------------------------------------------------------------

 cout << "Before Union" << endl;

 for(int i=0; i<info1.size()-1; i++) // Printing out list til second to last
    {
     cout << info1[i] << ", "; // Output
    }
 cout << info1.back() << "." << endl; // Prints last item for proper format

 for(int i=0; i<info2.size()-1; i++) // Printing out list til second to last
    {
     cout << info2[i] << ", "; // Output
    }
 cout << info2.back() << "." << endl << endl; // Prints last item for proper format

//-----------------------------------------------------------------------------
 cout << "Union" << endl << endl;

 set_union(info1.begin(), info1.end(), info2.begin(), info2.end(), info3.begin()); // Union
 info3.resize(it-info3.begin()); 

 cout << " After Union" << endl << endl;

 for(int i=0; i<info3.size()-1; i++) // Printing out list til second to last
    {
     cout << info3[i] << ", "; // Output
    }
 cout << info3.back() << "." << endl; // Prints last item for proper format

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

cin.ignore(); // Pause 
cin.get();
return 0; // Success
}
//----------------------------------------------------------------------------- 


Output before Crash:

C:\Users\Zero>g++ BolerCSC102Homework7.cpp

C:\Users\Zero>a.exe data60i.dat data61i.dat

data60i.dat is open...


data61i.dat is open...

 Items within File 1
---------------------
1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 6, 7, 8, 9, 45, 66, 455.

Size of Array 1: 20


 Items within File 2
---------------------
1, 2, 2, 3, 3, 4, 5, 5, 6, 8, 8, 9, 16, 24, 45, 45, 49, 66, 88, 455.

Size of Array 2: 20

Before Union
1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 6, 7, 8, 9, 45, 66, 455.
1, 2, 2, 3, 3, 4, 5, 5, 6, 8, 8, 9, 16, 24, 45, 45, 49, 66, 88, 455.

Union


C:\Users\Zero>
Line 116: Where do you initialize it? An uninitialzied iterator is guaranteed to cause a crash.
Should I initialize it to the beginning of Info3.begin()? I've only used extremely simple vectors, so I'm unsure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator> // for std::back_inserter

int main ()
{
    std::vector<int> info1 = { 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 6, 7, 8, 9, 45, 66, 455 } ;
    std::vector<int> info2 = { 1, 2, 2, 3, 3, 4, 5, 5, 6, 8, 8, 9, 16, 24, 45, 45, 49, 66, 88, 455 } ;

    std::vector<int> info3 ; // empty vector

    // std::back_insert_iterator adds elements to the end of info3
    // http://en.cppreference.com/w/cpp/iterator/back_inserter
    std::set_union( info1.begin(), info1.end(),
                    info2.begin(), info2.end(),
                    std::back_inserter(info3) ) ; // ********
    
    if( !info3.empty() )
    {
        for( std::size_t i=0; i<info3.size()-1; i++ ) // Printing out list til second to last
            std::cout << info3[i] << ", "; // Output
            
        std::cout << info3.back() << ".\n" ; // Prints last item for proper format
    }
}

http://coliru.stacked-crooked.com/a/f76a923ea97cc3fc
I used the back inserter, that's a pretty simple and useful tool. Thanks for the help
Last edited on
Topic archived. No new replies allowed.