Trouble with vectors

OK I have this class of objects that are sheep. They have different values like color cost etc... I successfully loaded them into a vector to list and sort them, but now I want to pick them out of that vector and into a ship vector. Here is my code so far:

//print the shipping manifests
void printManifests(vector<sheep> &list)

{

if (numOfSheepToDest() > 0) {
vector<sheep>::iterator itrSheep;
vector<dest>::iterator itrDest;

for ( itrDest=portList.begin();itrDest!= portList.end();itrDest++)
{


for (itrSheep=list.begin(); itrSheep!=list.end(); itrSheep++)
{
if ( itrDest->portName == (*itrSheep).getDestination())
{


/* this line below is the trouble.
How do I pass the class of
sheep to this new vector shipVect?*/


shipVect.push_back(list[itrSheep]);

}

}

}
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>

struct sheep
{
    int id ;
    std::string destination ;
    // ....
};

struct destination
{
    std::string port_name ;
    // ....
};

std::vector<sheep> make_shipping_list( std::vector<sheep> sheep_list, std::vector<destination> port_list )
{
    // sort sheep_list on destination
    std::sort( std::begin(sheep_list), std::end(sheep_list),
               [] ( sheep a, sheep b ) { return a.destination < b.destination ; } ) ;

    // sort port_list on port_name
    std::sort( std::begin(port_list), std::end(port_list),
               [] ( destination a, destination b ) { return a.port_name < b.port_name ; } ) ;

    std::vector<sheep> shipping_list ;

    auto sheep_iter = std::begin(sheep_list) ;
    for( const destination& dest : port_list ) // for each port in port list
    {
        // skip over the sheep bound for destinations before this destination
        while( sheep_iter != std::end(sheep_list) && sheep_iter->destination < dest.port_name ) ++sheep_iter ;

        // add the sheep bound for this particular destinations to the shipping list
        while( sheep_iter != std::end(sheep_list) && sheep_iter->destination == dest.port_name )
        {
            shipping_list.push_back( *sheep_iter ) ;
            ++sheep_iter ;
        }
    }

    return shipping_list ;
}

int main()
{
    const std::vector<sheep> sheep_list =
    {
        { 1, "Gibralter" }, { 2, "Kobe" }, { 3, "Aberdeen" }, { 4, "Oslo" }, { 5, "Alexandra" },
        { 6, "Alexandra" }, { 7, "Gibralter" }, { 8, "Tripoli" }, { 9, "Antwerp" }, { 10, "Aberdeen" },
        { 11, "Malmo" }, { 12, "Hull" }, { 13, "Oslo" }, { 14, "Kobe" }, { 15, "Alexandra" }, { 16, "Tripoli" }
    };

    const std::vector<destination> port_list = { { "Tripoli" }, { "Aberdeen" }, { "Alexandra" }, { "Antwerp" } } ;

    for( sheep s : make_shipping_list( sheep_list, port_list ) )
        std::cout << "sheep# " << std::setw(2) << s.id << " to " << s.destination << '\n' ;
}

http://coliru.stacked-crooked.com/a/a31b860e8daeea28
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>

struct sheep {
    int id ;
    std::string destination ;
    // ....
};

struct destination {
    std::string port_name ;
    // ....
};

void make_shipping_list ( std::vector<sheep>& sheep_list, const std::vector<destination>& port_list ) {
	auto it = std::remove_if (sheep_list.begin(), sheep_list.end(), [&](sheep s)->bool {return std::find_if(port_list.begin(), port_list.end(), 
		[&s](destination d)->bool {return s.destination == d.port_name;}) == port_list.end();});
	sheep_list.erase (it, sheep_list.end());
	std::sort( std::begin(sheep_list), std::end(sheep_list), [] ( sheep a, sheep b ) { return a.destination < b.destination ; } ) ;
}

int main() {
	std::vector<sheep> sheep_list = {
        { 1, "Gibralter" }, { 2, "Kobe" }, { 3, "Aberdeen" }, { 4, "Oslo" }, { 5, "Alexandra" },
        { 6, "Alexandra" }, { 7, "Gibralter" }, { 8, "Tripoli" }, { 9, "Antwerp" }, { 10, "Aberdeen" },
        { 11, "Malmo" }, { 12, "Hull" }, { 13, "Oslo" }, { 14, "Kobe" }, { 15, "Alexandra" }, { 16, "Tripoli" }
    };
    const std::vector<destination> port_list = { { "Tripoli" }, { "Aberdeen" }, { "Alexandra" }, { "Antwerp" } } ;
	make_shipping_list (sheep_list, port_list);
    for( sheep s : sheep_list )
        std::cout << "sheep# " << std::setw(2) << s.id << " to " << s.destination << '\n' ;
}

Output:
1
2
3
4
5
6
7
8
sheep#  3 to Aberdeen
sheep# 10 to Aberdeen
sheep#  5 to Alexandra
sheep#  6 to Alexandra
sheep# 15 to Alexandra
sheep#  9 to Antwerp
sheep#  8 to Tripoli
sheep# 16 to Tripoli 


Not sure which of the two solutions is more efficient though.
Last edited on
N number of sheep, M number of ports and K number of entries in the shipping list
The first one is O( N log N + M log M )
The second one is O( N * M + K log K )
Last edited on
Thanks a bunch I'm going to study what you did here, but I actually got it working with the following. Not sure if it's the most efficient or proper way. Also don't know if I have any bugs past 10 entries because that is the max per ship.

//print the shipping manifests
void printManifests(vector<sheep> &list)

{

if (numOfSheepToDest() > 0)
{
sheepShip ss;
bool justMadeNewShip = false;
vector<sheep>::iterator itrSheep;
vector<dest>::iterator itrDest;
for ( itrDest=portList.begin(); itrDest!= portList.end(); itrDest++) // going through port vector
{
for (itrSheep=list.begin(); itrSheep!=list.end(); itrSheep++) // going through sheep vector

{
if ( itrDest->portName == (*itrSheep).getDestination()) // see if destination port names are the same

{
if (ss.loadSheep(*itrSheep))
cout << "Success one sheep loaded!\n";
else
{

cout << "Ship is full making new ship!\n;";

shipVect.push_back(ss);
ss.unloadSheep(ss.sheepCount());
justMadeNewShip = true;

}




}


}
if (justMadeNewShip == false)
{
shipVect.push_back(ss);
ss.unloadSheep(ss.sheepCount());

}
justMadeNewShip = false;




}
// ss.printSheep();
//shipList.clear(); // erase the ship list

portList.clear(); //erase the port list
}
}

//clear the shipping list of sheep
void clearList(vector<sheep> &list)
{





list.clear();

}
Well I thought I had it licked, but I'm having trouble accessing the sheep class object that's inside the ship class that is inside a vector. I can not access the functions and variables in the sheep class.

Here is that code again with the error:

//print the shipping manifests
void printManifests(vector<sheep> &list)

{

if (numOfSheepToDest() > 0)
{
sheepShip ss;
bool justMadeNewShip = false;
vector<sheep>::iterator itrSheep;
vector<dest>::iterator itrDest;
vector<sheepShip>::iterator itrShip;
// int totalShips = 0;
for ( itrDest=portList.begin(); itrDest!= portList.end(); itrDest++) // going through port vector
{
// totalShips = ceil(itrDest->sheepCount / 10.0);
// int counter = 0;
for (itrSheep=list.begin(); itrSheep!=list.end(); itrSheep++) // going through sheep vector

{
if ( itrDest->portName == (*itrSheep).getDestination()) // see if destination port names are the same

{
if (ss.loadSheep(*itrSheep))
cout << "Success one sheep loaded!\n";
else
{

cout << "Ship is full making new ship!\n;";

shipVect.push_back(ss);
ss.unloadSheep(ss.sheepCount());
justMadeNewShip = true;

}




}


}
if (justMadeNewShip == false)
{
shipVect.push_back(ss);
ss.unloadSheep(ss.sheepCount());

}
justMadeNewShip = false;




}
// ss.printSheep();


for (itrShip=shipVect.begin(); itrShip!=shipVect.end(); itrShip++) // going through sheep vector

{
(*itrShip).print(); //ERROR right HERE!



// C:\MyCPrograms\lab9\main.cpp|313|error: 'class sheepShip' has no member named 'print'|


}

//shipList.clear(); // erase the ship list

portList.clear(); //erase the port list
}



}
> (*itrShip).print(); //ERROR right HERE!

shipVect contains objects of type sheepShip
An object of type sheepShip presumably contains an object of type sheep

Either provide a print() function for sheepShip: void sheepShip::print() const ;

or provide a function which allows access to the sheep object inside: const sheep& sheepShip::the_sheep() const ;
And then: itrShip->the_sheep().print() ;
Topic archived. No new replies allowed.