Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Map of Vector of Structs?

Tags:

c++

map

vector

So here's a snippet of my code:


    struct dv_nexthop_cost_pair
    {
      unsigned short nexthop;
      unsigned int cost;
    };

map<unsigned short, vector<struct dv_nexthop_cost_pair> > dv;

I'm getting the following compiler error:

error: ISO C++ forbids declaration of `map' with no type

What's the proper way to declare this?

like image 830
meteoritepanama Avatar asked Apr 17 '10 06:04

meteoritepanama


People also ask

What is a struct in C++?

Since structures are multivalue, multitype data objects, they can be considered to form a useful tool in manipulation of quantities such as vectors, complex variables, etc. For this we define functions of the type struct, i.e., the return value of the function is a structure. The function may have arguments of type struct besides other arguments.

Why vector vector elements are placed in contiguous storage?

Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. Map of Vectors in STL: Map of Vectors can be very efficient in designing complex data structures.

What is vector of maps in STL?

Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. Vector of Maps in STL: Vector of maps can be used to design complex and efficient data structures.

How to initialize a vector of structs in C++?

Use Range Constructor to Initialize a Vector of Structs in C++ Alternatively, the range constructor can be utilized to initialize the vector of structs. This method is useful when one needs to create another copy of the existing vector object.


1 Answers

Either you forgot to #include the right headers or didn't import the std namespace. I suggest the following:

#include <map>
#include <vector>

std::map<unsigned short, std::vector<struct dv_nexthop_cost_pair> > dv;
like image 143
Marcelo Cantos Avatar answered Oct 12 '22 08:10

Marcelo Cantos