Problems using std::vector with boost::any in C++ - c++

I am trying to create a vector where I will be storing different types of elements (in my case multidimensional arrays) in C++.
I can store the multi-dimensional arrays in the vector but when I try to retrieve through cast::any I receive errors. Let me illustrate all these with a very short example:
#include <iterator>
#include <string>
#include <iostream>
#include <algorithm>
#include <list>
#include <boost/any.hpp>
#include "boost/variant.hpp"
#include "boost/multi_array.hpp"
using namespace std;
using blaze::DynamicMatrix;
using boost::multi_index_container;
using namespace boost::multi_index;
main(){
//Creation of a 3-dimensional array of int
typedef boost::multi_array<int, 3> array_type;
array_type C(boost::extents[3][4][2]);
C[1][1][1] =222;
//Creation of vector and insertion of the 3-d array in it
std::vector<boost::any> vector;
vector.push_back(C);
// Trying to acess a stored element of the array from the vector
cout << endl << "print element C[1][1][1] = " <<boost::any_cast<array_type24> (vector[0][1][1][1]) ;
return 1;
}
So, when I run this I receive the following error:
"no match for ‘operator[]’ (operand types are ‘__gnu_cxx::__alloc_traits >::value_type {aka boost::any}’ and ‘int’)"
Any ideas how to use correctly the boost::any_cast or what I am doing wrong, would be very much appreciated.

Related

Right way to initialize and introduce vector of arrays in one line?

What are the right methods?
How to avoid the 3 errors?
I tried the followings:
#include <vector>
#include <array>
#include <iostream>
using namespace std;
struct s_4{double x,z,k,wsize;};
typedef vec4 vector <array<double,4>>; //ERROR #1
void main()
{
vector <s_4> s1;
vector <array<double,4>> d1;
s1.push_back(*new (s_4){10.0,11,1,0.25e-3}); //OK
d1.push_back(*new (array<double,4>){10.0,11,1,0.25e-3}); //OK
d1.push_back(*new (double[4]){10.0,11,1,0.25e-3}); //ERROR #2
vector <array<double,4>> d2{11,12,13,14.1}; //ERROR #3
getchar();
}
It is like it is very difficult to use large arrays in vectors
The correct code is:
#include <vector>
#include <array>
#include <iostream>
using namespace std;
struct s_4{double x,z,k,wsize;};
typedef vector <array<double,4>> vec4;
int main()
{
vector <s_4> s1;
vector <array<double,4>> d1;
s1.push_back({10.0,11,1,0.25e-3});
d1.push_back({10.0,11,1,0.25e-3});
d1.push_back({10.0,11,1,0.25e-3});
vector <array<double,4>> d2{{11,12,13,14.1}};
return 0;
}
Your first error in the typedef is that the name of the typedef comes last.
Your first three push_backs were leaking memory, you don't need to name the type when initialising.
The second error is because a c array can't be converted directly to a std::array.
The last needs two sets of braces, one to initialise the vector and one to initialise each array.
In addition to Alan's answer:
Why are you trying to allocate your arrays on the heap? You could place your arrays on the stack and use initializer lists:
#include <vector>
#include <array>
#include <iostream>
int main()
{
std::vector <std::array<double,4>> data = {
{10.0,11,1,0.25e-3},
{10.0,11,1,0.25e-3},
{10.0,11,1,0.25e-3},
{11,12,13,14.1}
};
}
However, initializer lists are a C++11 feature so you may compile with -std=c++11:
g++ -g -Wall -O2 -std=c++11 test.cpp -o test
Furthermore you should avoid using namespace std, as this may cause problems if you use additional libraries that implement for example vectors for mathematical calculations.

C++ vector of array of strings?

I want to have a dynamic structure which I could iterate on, there will be unknown number of entries and known number of strings for each entry. I thought that vector of array of strings could be the way, however I get error while compiling this:
vector< array<string, 5> >
error: invalid use of incomplete type 'struct std::array<std::basic_string<char>, 5u>'
What am I doing wrong? and if this is kind of the way - how would I add/get values to/from this structure?
Did you include all these three headers?
#include <vector>
#include <array>
#include <string>
This compiles just fine:
#include <vector>
#include <array>
#include <string>
int main(int argc, char const *argv[])
{
std::vector<std::array<std::string, 5> > myVec;
return 0;
}

error: request for member 'clear' in 'graph', which is of non-class type

When I try to compile the following code:
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define pii pair<int, int>
vector<pii> graph[100001];
int main() {
graph.clear();
return 0;
}
I get this error:
error: request for member 'clear' in 'graph', which is of non-class type 'std::vector<std::pair<int, int> > [100001]'
Any help is appreciated.
In your code the graph is an array of 100001 vectors of pairs of ints, not std::vector<T>.
You can however represent graphs like below:
#define pii std::pair<int, int>
std::vector<std::vector<pii> > graph(100001);
graph.clear();
If you really really want to use raw arrays, you can very inefficiently clear your graph like below:
std::fill_n(graph, 100001, std::vector<pii>());

How to load Multidimensional array values into vector?

This is part of the code (header and the main part):
#include <iostream>
#include <sstream>
#include <string>
#include <gl\GL.h>
#include <gl\GLU.h>
#include <glut.h>
#include <RassHost.h>
#include <api\iomap.h>
#include <api\iotrans.h>
#include <api\cgeometry.h>
#include <vector>
using namespace std;
int main()
{
cout << "Enter IP: " << endl;
getline(cin, server_ip);
enum(KEY_L = 'A', KEY_R = 'D', KEY_RUN = 'WW', KEY_JUMP='SPACE');
typedef OBJECT_3D_SYS_TYPES_NUM OBJECT3D_RCN_TYPE;
OBJECT3D_RCN_TYPE _psyObjects[][] = getPsyhicsPartObjects();
vector<OBJECT3D_RCN_TYPE> _objects;
//I would like to load _psyObjects[][] into vector<OBJECT3D_RCN_TYPE> _objects;
Server::StartGame(Server::getIP(), 8888, "-r run", false);
system("pause");
return 0;
}
Is it possible to copy _psyObjects values into vector<OBJECT3D_RCN_TYPE>?
I want to control the multidimensional array with vector api, if it is possible.
Thanks!
You'll need to create a vector of vectors:
vector< vector<OBJECT3D_RCN_TYPE> > _objects;
Then just fill it like a normal vector.
I'd post more code, but you need to know the dimensions of the array, and I can't see those from the code.
You could also use a Boost::multi_array. It's api is like std::vector's, but possibly similar enough to meet your needs.

How to declare vectors in C++?

I'm trying to use a vector of strings in my code instead of an array of strings but apparently I miss some detail in the declaration of the vector. Using the following code, I get this error: ‘vector’ was not declared in this scope
// Try to implement a vector of string elements
#include<iostream>
using namespace std;
int main() {
const int MAX_ITEMS = 10;
vector<string> my_vector(MAX_ITEMS);
return 0;
}
How should I correctly declare the vector?
You should add these includes:
#include <vector>
#include <string>
You have to include the header:
#include <vector>
#include <string>
You need:
#include <vector>