Inserting into a double vector

Hello everyone!

I searched everywhere for this, but found nothing... What I need to know is how to insert an integer value in a double vector. In double arrays, we would simply write, for example:
1
2
int array[10][10];
array[0][0] = -1;


I am going to post part of the code, as I am sure that the problem lies in this part only.
This just for initializing the vector...
1
2
3
4
vector< vector< int > > matrix;
vector< int > clause;
for (int j = 0; j < numLit; j++) clause.push_back(0);
for (int i = 0; i < numClause; i++) matrix.push_back(clause);


Now this is how I "tried" to insert into the double vector... But it turned out that my program crashed. I isolated everyline, and the last line in the for loop is causing the program to crash.
1
2
3
4
5
6
7
for (int i = 0; i < varcount; i++)
            {
                int column = abs(temp[i]);
                int row    = linecount - 1;
                int value  = (temp[i] > 0 ? 1 : 2);
                matrix[column][row] = value;
            }


I repeat my question, how can I insert into a double vector?

Thanks!
Last edited on
Just use the same syntax for a 2D array:
 
matrix[i][j] = 0;

Because you have a vector of vectors then saying matrix[i] returns a vector which you can then dereference using [j].
Last edited on
Okay, fine, I can insert into a double vector using the "array index" synatx...

But I found something out...
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
while ( !inData.eof() )
    {
        getline (inData, str);
        if (linecount == 1)
        {
            sscanf(str.data(), "p cnf %d %d", &numClause, &numLit);
            for (int j = 0; j < numLit; j++) clause.push_back(0);
            for (int i = 0; i < numClause; i++) matrix.push_back(clause);
        }
        if (linecount > 1 && linecount < numClause)
        {
            spacecount_ = 0;
            for(int i = 0; i != str.size(); i++) spacecount_ += ( str[i] == ' ');
            varcount = spacecount_ + 1;
    

            int *temp;
            temp = new (nothrow) int [varcount];
            tempfile.open("fout.txt");
            tempfile << str;
            tempfile.close();

            int number;
            tempfile_.open("fout.txt");
            for (int i = 0; i < varcount; i++) tempfile_ >> temp[i];
            tempfile_.close();
            }


            for (int i = 0; i < varcount; i++)
            {
                int column = abs(temp[i]);
                int row    = linecount - 1;
                int value  = (temp[i] > 0 ? 1 : 2);
                matrix[column][row] = value;
            }
            delete [] temp;


in the case where linecount = 0 I WANT to extend the vector to hold 12 elements by 15 elements (number of clauses = 12 and number of literals = 15)... So, basically this if statement will execute once, I tried taking out this:
1
2
for (int j = 0; j < numLit; j++) clause.push_back(0);
for (int i = 0; i < numClause; i++) matrix.push_back(clause);

outside the while loop and punch in 12 and 15 for numClause and numLit respectively, and it worked, but HOW can I inside the loop, while I am reading from a file, resize the double vector?
Thanks!
Last edited on
To extent the size in the j direction you need to loop through each array in the i direction and extend it individually:
1
2
3
4
for(size_t i = 0; i < matrix.size(); ++i)
{
	matrix[i].resize(new_j_size);
}

But to extend it in the i direction you can do that like this:
 
matrix.resize(new_i_size, std::vector<int>(current_j_size));
Last edited on
Topic archived. No new replies allowed.