0

I'm new in c++ programming and I'm trying to understand better the differences between array and vector. In my program I've got a class Graph with some arrays as private members. The class has a method that use these arrays to implement the Prim's algorithm for minimum spanning tree. I took the algorithm from this page and changed it for my program.

I am now asked to use vectors instead of arrays so I would like to know:

How many things do I really have to change? The declaration and the constructors, ok. But the cycles, the initializations. Do I have to change everything? The program works anyway. Is it so important to use vector's function?

2
  • 1
    Show us some code and indicate which array should be converted to a vector. Commented Nov 11, 2013 at 14:52
  • 1
    Hard to say without seeing any code. I would change the declarations first and see how it goes from there. Commented Nov 11, 2013 at 14:54

3 Answers 3

1

If you want to stick with current implementation but you would like to experiment with vectors you could populate vector with array data this way:

vec.assign(arr, arr + arr_size);

Sign up to request clarification or add additional context in comments.

1 Comment

This worked! This is the so-called iterator construction a vector from arrays - the iterator constructor can also be used to construct from arrays
0

I took a look at the code at your link above and spotted quite a few places where STL vectors can be used rather than the arrays that are defined.

For example, below is an example of how to replace a couple of the arrays with vectors, and then initialize their values:

#include <iostream>
#include <limits>
#include <vector>

// Number of vertices in the graph
static const int V = 5;

int main()
{
    // Rather than use:
    // int key[V];
    // bool mstSet[V];

    // You could setup STL vector containers:
    std::vector<int> key;
    std::vector<bool> mstSet;

    // Next, initialize:
    for (size_t i = 0; i < V; i++) {
        key.push_back(INT_MAX);
        mstSet.push_back(false);
        std::cout << key[i] << "; " << mstSet[i] << std::endl;
    }

    return 0;
}

2 Comments

<limits.h> is C, the C++ equivalent is <limits>. Don't use #defines, use const. Avoid other C libraries like stdio. Avoid using namespace std
@Manu343726, your exactly right. I cleaned up the code to better illustrate good C++ practice. Thanks for your comments.
0

It should be very simple to switch from arrays to a vector. You could initialise the vector with the size of your array and fill it with 0 elements:

vector<YourType> v(size_of_array, YourType(0));

and then use v more or less as you used your array, i.e.:

v[x] = YourType(y);
// ....
f(v[z]);

etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.