Multidimensional Vector

I want to clear a Multidimensional vector but "clear()" function isn't working.Can anybody help me?
If you clear the outer vector, all inner vectors are destroyed - is this not what you want? In what way is the clear function not working?
there's a flaw in Dev C++ compiler. So, it doesn't work on it
Then don't use the old, outdated Bloodshed Dev-C++ compiler.
Old is gold. Dev C++ is good in many ways irrespective of its flaws.
Dev-C++ has been updated since the old Bloodshed version that didn't even comply with the standard.
Yes. But still they weren't able to fix the problem.
Then don't use Dev-C++ if you want a working compiler? I don't understand where the confusion is here.
Thats the only solution hoity :(
Did you just assume the OPer is using Dev-C++? Or did he mention it elsewhere? It just seems like a random assumption.
I use Dev C++ and clear() wont work on it. and its obviously a random assumption.
What do you mean by "clear() wont work on it"? Can you post the code you tested with? It might not be a bug with Dev-C++
I am using the latest version of codeblocks 12.11

1
2
3
4
5
6
7
8
9
10
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

#define MAX 100000

vector<int>graph[MAX];

here I declare the vector graph globally

then in main function I want to destroy them
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    int node,edge,x,y,source;
    while(cin>>node)
    {
        if(node==0)
        {
            return 0;
        }
        cin>>edge;
        for(int i=0;i<edge;i++)
        {
            cin>>x>>y;
            graph[x].push_back(y);
            graph[y].push_back(x);
        }
        bfs(node,x);
       
        graph.clear();
        
    }

    return 0;
}


then my IDE showing an error that request for member 'clear' in graph which is non-class type std:: vector

But if I change my clear section to

1
2
3
4
5
 
for(int i=0;i<graph[i].size();i++)
        {
            graph[i].clear();
        }

then it works.
Do you have any idea, how to clear a multidimensional vector without using loop
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>

int main()
{
    const std::size_t MAX = 100000 ;

    // std::vector<int> graph[MAX] ; // arraY of MAX vectors
    std::vector< std::vector<int> > graph(MAX) ; // vector of MAX vectors

    // ...

    graph.clear() ;
}

What's wrong with my code
graph isn't a "multidimensional vector". It's an array of vectors. In other words, graph is simply a pointer to a vector. You can't call clear on it, because it's not an object with a clear method, or any methods at all.

Edit: JLBorges post shows you how to make it a multidimensional vector - i.e. a vector of vectors.
Last edited on
got it.Thanks
Topic archived. No new replies allowed.