indeterminate vector

Lets say you want to make a vector of indeterminate size and set the location [1][5] equal to the integer 3 and the location [7][2] equal to the integer 4, but leave all the other possible locations not equal to anything. For example, lets say you may want to add additional locations later but don't know how many in the end you will. Is it possible to do this with vector< vector<int> > vec or would you need to use something other than #include <vector>? If so how would it be done? Can you show some sample code?
Last edited on
Can you describe the details of what exactly it is you're trying to accomplish? Because to me, it sounds like you're going about things the wrong way.
> something other than #include <vector>?
> If so how would it be done? Can you show some sample code?

std::map<>, perhaps. http://www.cplusplus.com/reference/map/map/

Sample code:
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 <map>
#include <iomanip>

int main()
{
    std::map< std::size_t, std::map< std::size_t, int > > m ;

    m[1][5] = 15 ;
    m[7][2] = 72 ;
    m[3][5] = 35 ;
    m[6][0] = 60 ;
    
    std::cout << "'x' => no value has been set\n----------------------------------\n" ;
    
    for( std::size_t row = 0 ; row < 8 ; ++row )
    {
        const auto irow = m.find(row) ;

        for( std::size_t col = 0 ; col < 8 ; ++col )
        {
            if( irow != m.end() ) // if the row has some entries
            {
                const auto icol = irow->second.find(col) ;
                if( icol != irow->second.end() ) // and there is an entry at [row][col]
                    std::cout << std::setw(3) << icol->second << ' ' ;
                else
                    std::cout << "  x " ;
            }
            else std::cout << "  x " ;
        }

        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/4f0b65ff0074d16b
Topic archived. No new replies allowed.