help with using a set()!!

I have an additional txt file that i need to read into my cpp file by using a set. Im not understanding how to incorporate this into my code. any help would be great.

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
  #include "city.h"
  7 #include <iostream>
  8 #include <iomanip>
  9 #include <fstream>
 10 #include <sstream>
 11 #include <vector>
 12 using namespace std;
 13 
 14 vector<City*> cityvec;
 15 
 16 int main()
 17 { 
 18   ifstream inf("inputfile.txt");
 19   string city;
 20   float rainOne, rainTwo, totalrain;
 21   cout << "City\t\t total\n"
 22     << "--------------------\n"
 23     <<setprecision(1) << endl;
 24   
 25     while (!inf.eof())
 26     {
 27       inf >> rainOne;
 28       inf.ignore();
 29       getline(inf, city, ',');
 30       inf >> c;
 31       inf.ignore();
 32      City* ctyp;
 33      ctyp= new City(rainOne, city, raintwo);
 34      cityvec.push_back(ctyp);
 35      
 36      
 37      totalrain=(ctyp->getRainOne()+ctyp->getRainTwo());
 38      setw(21+(city.size()));
 39      
 40      
 41      cout << ctyp->getCityName();
 42      cout.width(19-City.size());
 43      cout <<' ' <<totalrain <<endl;
 44    }  
 45 return 0;
 46 }
closed account (N85iE3v7)
Take a look here:

http://www.cplusplus.com/reference/set/set/

I bet they are asking you to replace the usage of the vector container for the set container, set is an associative container, and it won't accept duplicated entries.

1
2
3
4
// this is what you are using
vector<City*> cityvec;
// include <set>
// use std::set<City*> cityset; 


I would also supply a functor to your City class, so that the constructor of set knows how to sort it at every insertion. It is something simple like:

1
2
3
4
struct classcomp {
  bool operator() (const City1& c1, const City2& c2) const
     // return your own way of comparing City objects;  
};


Then you construct your set passing the above functor:

std::set<City*,classcomp> cityset;

You would usually use a set when you want to have unique entries and want it sorted automatically during the insertion. Please if you still have questions, post your city class here.

Hope it helps.
Last edited on
closed account (N85iE3v7)
Hey

It is something like this

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
#include<set>
#include<iostream>
#include <string>

using namespace std;

class City
{
public:
City( float f1,
float f2,
string thecity);

string getCityName() ;
float getRainOne();
float getRainTwo() ;

private:

float rainOne;
float rainTwo;
string cityName;

};

City::City( float f1,
float f2,
string thecity
) : rainOne(f1), rainTwo(f2), cityName(thecity)
{
// for(int i=0;i<50;++i);
}

string City::getCityName()
{
return cityName;
}

float City::getRainOne()
{
return rainOne;
}

float City::getRainTwo()
{
return rainTwo;
}

template< class D >
struct comp
{
bool operator()( const D& c1, const D& c2) const
{
return ( c1->getCityName().length() > c2->getCityName().length() );
};

};

int main()
{

set<City*, comp<City*> > setContainer;
typedef set<City*, comp<City*> >::const_iterator ItCpp;

City* c1 = new City( 2.2, 2.3, "Piramboia");
City* c4 = new City( 3.3, 6.0, "Jarasdasdasdasdasdasdasdasdasd");
City* c2 = new City( 2.4, 2.3, "Carapicuiba");
City* c3 = new City( 2.4, 2.3, "Pindamongaba");

setContainer.insert( c1 );
setContainer.insert( c2 );
setContainer.insert( c3 );
setContainer.insert( c4 );

//for( auto it = setContainer.begin(); it != setContainer.end(); it++)// C++11
for( ItCpp it = setContainer.begin(); it != setContainer.end(); it++)// C++03
{
City* ptr = *it;
cout << "City: " << ptr->getCityName() << endl;
}

return 0;

}


A bit tricky , but I sorted the City class by its name length. Also, the pointer notation is messing. The const operator indicates that the variable referenced by the operator & ( an improved pointer reference that Cpp allows ) cannot be changed. The const after the method declaration simply means it won't change the value of any class members.
Why use a compare struct when you can use an std::function<...>?

1
2
3
4
5
6
7
8
9
10
11
12
#include <set>
#include <functional>

int main()
{
    typedef std::function<bool(const int &, const int &)> cmpf;
    
    cmpf compare = [](const int& lhs, const int& rhs){return lhs < rhs;};
    std::set<int, cmpf> s(compare);
    
    return 0;
}
closed account (N85iE3v7)
@Bourgond, agreed. I was just not sure if the OP was using a C++11 compliant compiler. Using lambda functions would confuse the Op even more, but I concur that is a clear and better solution.
Last edited on
Topic archived. No new replies allowed.