Why we cannot declare vector< int, pair< int, int > > - c++

In a certain competitive coding, i need to use a data-structure mentioned above but it gave me an error, why it threw an error and which is the simplest data-structure i can use for this

A vector only contains one type. You are trying to put two into the vector. You could instead do vector<pair<int, pair<int, int>> and that would be fine.

We can not declare vector as vector < int , pair < int , int > > Because Vector is a sequence of contiguous storage of elements of same type .
And this represents the associative container having key value property like mapping .
so We cant declare vector as vector< int , pair < int ,int > > .
We can declare it as vector< pair < int , pair < int , int > > or vector< data_type>

Related

What is the time complexity of sorting datatype vector< pair<int, vector<int> > > ? ( As the swap needs to swap vectors ))

My datatype and sort function look like below:
vector< pair< int, vector<int> > > data;
sort(data.begin(),data.end(),[]( auto a,auto b )
{
return a.first<b.first;
});
It will need to swap vectors too. Does the swap operation need to copy and paste all the elements of the vectors making the complexity of swap linear to the length of the two vectors to be swapped? Or will it do some trick (ex: pointer operation) to make it a constant time operation?

How to print a nested std::map using gdb?

My program has a nested std::map object like
map < int, set < void *> *> m;
My program coredown in some situation, and I don't know the reason.
I suspect the problem is in the nested map.
I only know using pmap to print simple map objects like std::map<int, int>,
but don't know how to print more complicated map objects.
If a map is defined as map< int, int > m;, then
gdb pmap m int int
does work, but if a map is defined as
map< int, set < void * > * > m;
then
gdb pmap m int set < void *> *
does not work

How would one push back an empty vector of pairs to another vector?

std::vector<std::vector< std::pair<int, int> > > offset_table;
for (int i = 0; i < (offset.Width()*offset.Width()); ++i)
{
offset_table.push_back( std::vector< std::pair<int, int> > );
}
This is my code, but I am getting the errors:
main.cpp: In function ‘void Compress(const Image<Color>&, Image<bool>&, Image<Color>&, Image<Offset>&)’:
main.cpp:48:66: error: expected primary-expression before ‘)’ token
I do not want any values in the pairs, I just would like to have a vector of empty vectors at the moment. How would I do this?
You want to construct a vector to pass to push_back and you're just missing parentheses:
offset_table.push_back( std::vector< std::pair<int, int> >() );
Or, instead of your loop, you could just do the following. It's better because the vector will allocate just the right amount of memory in a single allocation:
offset_table.resize( offset.Width()*offset.Width(), std::vector< std::pair<int, int> >() );
Or this, which is more concise because it's using resize's default 2nd argument:
offset_table.resize( offset.Width()*offset.Width() );
std::vector<std::vector< std::pair<int, int> > > offset_table;
This is is a 2d array so you need to use nested array. For only getting the length if inner vector.
for(vector< pair<int, int >> vv in offset_table)
{
if(vv.size() == 0)
{
// this is your target.
}
}

Search in vector<std::pair<int, vector<int> > >

I would like to search within a vector<std::pair<int, vector<int> > >. This won't work due to the vector parameters:
std::vector<std::pair<int, std::vector<int> > > myVec;
iterator = find(myVec.begin(), myVec.end(), i);
Search would be on the first std::pair template parameter (int), in order to get the vector associated with it.
std::vector<std::pair<int, std::vector<int> > > myVec;
This requires C++0x for the lambda expression:
typedef std::pair<int, std::vector<int>> pair_type
std::find_if(myVec.begin(), myVec.end(), [i](pair_type const& pair)
{ return pair.first == i; });
If you're not using C++0x then either roll out your own loop or use something like Boost.Phoenix/Boost.Lambda.
Or, for both cases, why not use std::map?
You could make do with the following (pretty ugly) functoid:
struct FindFirst {
FindFirst(int i) : toFind(i) { }
int toFind;
bool operator()
( const std::pair<int, std::vector<int> > &p ) {
return p.first==toFind;
}
};
using it like this ( I couldn't get bind2nd to work - that's why I used the c'tor ):
int valueToFind = 4;
std::find_if(myVec.begin(), myVec.end(), FindFirst(valueToFind));
I think what you would like is a map:
std::map< int, vector< int > > foo;
You can then add elements, search by key etc:
int key = 4; //This will be the key
vector<int> value(5, 4); //Fill some values (5 4's in this case) into the vector
foo[key]=value; //Adds the pair to the map. Alternatively;
foo.insert( make_pair(key, value) ); //Does the same thing (in this context)
Looking at the way you've done things though, you might be wanting a std::multimap (which allows multiple values to have the same key) Class docs here
You're trying to map an int to a vector of int.
So try map<int, vector<int> >.
The second template parameter of a vector is the allocator - your compiler can probably puzzle out what you wanted to say, the declaration is wrong anyway. What you probably want is some sort of map type, like iammilind suggested.

C++ 5 dimensional vector?

I am trying to make a 5 dimensional vector and I can’t seem to get it to work.
I know if I need to write a 3 dimensional vector, I could write it in the following way:
vector< vector< vector<string> > > block(27, vector< vector<string> > (27, vector<string>(27)));
Then I call it: block[x][y][z] = “hello”;
I wrote the 5 dimensional vector in the following way and it gives me error.
vector< vector< vector< vector< vector<string> > > > > block(27, vector< vector< vector< vector<string> > > >(27, vector< vector< vector<string> > >(27, vector< vector<string> >(27, vector<string>(27)))));
Can you please tell me how to write a 5 dimensional vector in the right way?
Thanks a lot.
The final vector in your 5 dimensional array does not have a type that it is an array of.
vector< vector< vector< vector< vector > > > >
^^
Here. What is the base vector a vector off?
To make things easy to read a couple of typedefs would be nice:
typedef std::vector<std::string> Dim1;
typedef std::vector<Dim1> Dim2;
typedef std::vector<Dim2> Dim3;
typedef std::vector<Dim3> Dim4;
typedef std::vector<Dim4> Dim5;
Dim5 block(27, Dim4(27, Dim3(27, Dim2(27, Dim1(27)))));
Consider using the Boost Multidimensional Array Library for higher dimensional arrays.
http://www.boost.org/doc/libs/1_43_0/libs/multi_array/doc/user.html
"Boost MultiArray is a more efficient and convenient way to express N-dimensional arrays than existing alternatives (especially the std::vector> formulation of N-dimensional arrays). The arrays provided by the library may be accessed using the familiar syntax of native C++ arrays. Additional features, such as resizing, reshaping, and creating views are available (and described below)."
But you should stop and think if a dictionary would work better. If the data is sparse you'll save a ton of memory.
Create a key using the 5 dimensions, and create only the members you need.