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.
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;
}
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>());
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.
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>