Recommendations for iterating over either row or column?

Suppose I want to represent an apartment building in terms of floors and rooms with a single data object. Each floor has the same number of rooms at any given time. During the course of the program, I will need to change the number of floors occasionally and will need to change the number of rooms on each floor occasionally. (Don't worry about the building permits needed to do this.) Now, I also want to be able to iterate over either each floor (think "row") or each room (think "column"). For example, I might want to program the equivalent of "Johnson, go fix all the front doors on the 8th floor" or the equivalent of "Johnson, go fix all the front doors to room B on each floor". (Yeah, I know, for the latter I could iterate over each floor and then access room B but that doesn't feel as "clean" as being able to iterate over a separate room/column.) Does Anyone have any recommendations about how to approach this problem?
My initial feelings are to use vectors. This is because you don't know how many floors or rooms that you will have at any given time. You will also be reassigning the amount of floors and rooms that you have at any given time. You can do this with arrays, but not only is it harder it's more error prone. I would create a double-vector. The top level vector representing each floor and containing a vector with the room structs/classes.

I can't tell if this is a program that you are writing for school or real world use. For school, you probably don't need loading and saving. Therefore, your program will need to take your input every time it starts. I would ask about how many floors and then about how many rooms for each floor. If you have saving and loading functionality then you need think about a format to save and read the data into.

You may need a vector for your employees too. I'm on my phone right now, so I can't write any code. I hope this helped you get started.
You say you want to do this with a single object. You can certainly do that with a vector of vectors as MasterTheTurtle suggests.
1
2
3
4
5
 
struct Room 
{  // room variables here 
} 
vector<vector<Room>>  bldg;

However, I think you will soon find that you will have information that belongs at the flloor or building level. That being said, I think the proper representation is to have three classes. One each for Room, Flloor and Building.
1
2
3
4
5
6
7
8
9
10
11
12
13
class Room
{  // Room specific information and functions here
};

class Floor
{  vector<Room>  m_roomsl;
    //  Other floor specific information and functions here
};

class Building
{   vector<Floor>  m_floors;
     //  Other building specific informationn and functions here
};


@AbstractionAnon

Ah, I like that idea. It is certainly future proof in a good way. You can also assure that each vector is properly constructed and destructed. You can also have functions that modify the floors in an oop way instead of a c way.
You can also have functions that modify the floors in an oop way instead of a c way.

sorry to but in, but you can have oop in c. its not as easy, but its possible. what i think you meant is you can easily use oop as compared to c where you would most likely use functions
Well, yeah. I meant in a functional type of way. This is demonstrated in WinAPI
I think that it is good that he wont pass objects into functions. I think that is more error prone. Sometimes people accidentally create tons and tons of copies.
Last edited on
Interesting ideas. Is there a way, however, to say, "Give Me an iteratable collection of room B from all the floors" so as to be able to work with either the "row" or "column" of such an object?
> Is there a way, however, to say, "Give Me an iteratable collection of room B from all the floors"

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
#include <iostream>
#include <vector>
#include <functional>

struct room
{
     room( std::size_t f = 0, std::size_t n = 0, bool occ = false )
          : floor(f), number(n), occupied(occ) {}

     std::size_t floor ; // floor numbers start at 0
     std::size_t number ; // room numbers start at 0, 0 == 'A' 
     bool occupied = false ;
     // ...
};

std::ostream& operator<< ( std::ostream& stm, const room& r )
{
    stm << r.floor ;

    static constexpr char room_id[] = "ABCDEFGHIJKLMNPQRSTUVWXYZ" ;
    if( r.number < ( sizeof(room_id) - 1 ) ) stm << room_id[r.number] ;
    else stm << '-' << r.number ;

    if(r.occupied) stm << "(occupied)" ;

    return stm ;
}

struct building
{
    building( std::size_t floors, std::size_t rooms_per_floor )
                        : rooms( floors, std::vector<room>(rooms_per_floor) )
    {
        for( std::size_t f = 0 ; f < floors ; ++f )
            for( std::size_t r = 0 ; r < rooms_per_floor ; ++r ) rooms[f][r] = { f, r } ;
    }

    std::vector<room>& rooms_in_floor( std::size_t f ) { return rooms.at(f) ; }

    const std::vector< std::reference_wrapper<room> > room_across_floors( std::size_t r )
    {
        std::vector< std::reference_wrapper<room> > result ;
        for( auto& v : rooms ) result.push_back( std::ref( v.at(r) ) ) ;
        return result ;
    }

    private: std::vector< std::vector<room> > rooms ;
};

int main()
{
    building b( 8, 17 ) ;
    b.rooms_in_floor(2)[5].occupied = true ;
    b.rooms_in_floor(6)[5].occupied = true ;

    for( auto& r : b.rooms_in_floor(2) ) std::cout << r << ' ' ;
    std::cout << '\n' ;

    for( auto& r : b.room_across_floors(5) ) std::cout << r << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/e32d519bbb090ed3
Registered users can post here. Sign in or register to post.