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 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.
I want to fill a vector with the letters of the alphabet. So I wrote the following:
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
char i='a';
char f(){return i++;};
int main()
{
std::vector<char> lol(24);
std::generate_n(lol.begin(),lol.size(),f);
std::copy(lol.begin(),lol.end(),std::ostream_iterator<int>(std::cout,","));
}
But std::copy prints 97,98,99... the ASCII codes of the letters. How can I fix this?
Your ostream_iterator is <int> instead of <char> - Try changing it to <char>
change the template specialization to char :std::copy(lol.begin(),lol.end(),std::ostream_iterator<char>(std::cout,","));
im coming from PHP where i would do
$um['Im a string'][1] = 3;
for a 2d associative array where the first key is a string, the second an integer and the value is an integer as well. I try to do the same in c++. here is my attempt:
// experiment.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <unordered_map>
#include <vector>
#include <string>
using std::vector;
using std::string;
using std::unordered_map;
int _tmain(int argc, _TCHAR* argv[])
{
unordered_map <string,vector<int,int>> um;
um["Im a string"][1] = 3;
printf("Out: %d", um["Im a string"][1]);
return 0;
}
obviously its not the right syntax;
vector<int,int> isn't correct (vector isn't an associative container), you probably want a nested unordered_map<int>. So:
unordered_map <string,unordered_map<int,int>> um;
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>