![]() |
|
#1
|
|||
|
|||
How to initialize a 2d vector (after declaration)Hi all,
I would like to initialize a 2d vector in the class Graph.cpp according to an input "numNodes". Each element in the 2d vector must be an Arc object, which have attributes like "adj" and "weight". I dont know which is the better way to initialize that 2d vector. Please, could you help me? Here is my code and what I have tried. CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
In the main.cpp, I would like to call like: Graph *graph; graph->initGraph(4); graph->arcs[0][1].adj = true; graph->arcs[0][1].weight = 10; Thanks in advance for your help. |
|||
|
#2
|
|||
|
|||
Re: How to initialize a 2d vector (after declaration)Hi all,
Actually I got what I wanted. Just in case to help someone else. Here is the code! obs.: There is also question in the bottom! CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
In the main.cpp, we can use something like this: Graph *graph(4); graph->arcs[0][1].adj = true; graph->arcs[0][1].weight = 10; or Graph graph(4); graph.arcs[0][1].adj = true; graph.arcs[0][1].weight = 10; The question now is, Is there some performance difference in using a graph pointer or a graph object? |
|
#3
|
|||
|
|||
Re: How to initialize a 2d vector (after declaration)CPP / C++ / C Code:
CPP / C++ / C Code:
Quote:
Then again, somehow I feel like you're actually talking about the allocation part of the process. I used a really crude timer to 'benchmark' your code. What I did was this: CPP / C++ / C Code:
CPP / C++ / C Code:
Just for the heck of it I passed 20 instead of 4 to the Graph constructor and the blocks then took about 2 minutes both. Next I rewrote the constructor as CPP / C++ / C Code:
I further changed the constructor into CPP / C++ / C Code:
You could probably go even further. So, yes, new seems expensive, but if you really want to save, you need to consider how much you're actually saving with for example using CPP / C++ / C Code:
CPP / C++ / C Code:
With a dummy constructor (and of course no member accessing) the whole deal was 0.5 seconds for the above and 0.1 for the below block. Ps. I'd recommend ticking the "convert tabs into spaces" option in your editor if you haven't already. Pps. In C++ there's also a struct data type. It is equal to a class in every way expect the data is public by default. Seeing as you insist on using public data you might as well use a struct. |
|
#4
|
|||
|
|||
Re: How to initialize a 2d vector (after declaration)Hi Kimmo,
I really appreciate your effort and explanation on the performance. A big thank. |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| run script command on ns2.26 | newbie06 | Computer Software Forum - Linux | 66 | 16-Jan-2010 10:53 |
| Error C2374 | lolp1 | C++ Forum | 3 | 25-May-2008 16:40 |
| Multiple questions for C++ project | devster420 | C++ Forum | 1 | 20-Apr-2007 21:26 |
| The application failed to initialize properly (0xc0000005) | telstar2626436 | Computer Software Forum - Windows | 5 | 20-Dec-2005 01:01 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The