Like this question already asked, I'd like to initialize a container using STL where the elements are hard-coded in the cleanest manner possible. In this case, the elements are a doubly nested container:
set<vector<int> > A;
And I'd like (for example) to put the following values in:
A = [[0,0,1],[0,1,0],[1,0,0],[0,0,0]];
C++0x fine, using g++ 4.4.1. STL is preferable as I don't use Boost for any other parts of the code (though I wouldn't mind an example with it!).
This does use g++ 4.4.1, with -std=c++0x
#include <set>
#include <vector>
using namespace std;
int main()
{
set<vector<int>> A = {{0,0,1},{0,1,0},{1,0,0},{0,0,0}};
}
#include <boost/assign/list_of.hpp>
#include <vector>
#include <set>
using namespace std;
using namespace boost::assign;
int main()
{
set<vector<int> > A;
A = list_of
(list_of(0)(0)(1))
(list_of(0)(1)(0))
(list_of(1)(0)(0));
(list_of(0)(0)(0));
return 0;
}
Related
my question is pretty simple but I can't seem to find it out.
I want to know what libary to include when using stoi. I was using atoi and it works fine with
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
but I get "stoi not declared" when I run with stoi. Thanks
You need to #include <string> and use a compiler that understands C++11. Minimal example:
#include <string>
#include <cassert>
int main()
{
std::string example = "1234";
int i = std::stoi(example);
assert(i == 1234);
return 0;
}
Compile, for example, with g++ -std=c++11.
I am trying initializing sparseMatrix of Eigen, but it does not work when the initialization is in a class description. In the case of initialization in a function, not in a class, it works.
I am writting codes by C++ and using Visual Studio 2017.
I added
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW , but the problem remains.
#include <iostream>
#include <vector>
#include <Eigen/SparseCore>
#include <Eigen/Sparse>
#include "pch.h"
using namespace Eigen;
namespace A {
class A
{
std::size_t max_doc_id = 4;
std::size_t max_term_id = 4;
SparseMatrix<float, Eigen::RowMajor, int64_t> smat(max_term_id, max_doc_id);
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
I want to decide the size of smat matrix (col=4, row=4) , but the error message is like this (actually it is written in Japanese so it may not correct)
"the member A::A::max_term_id is not the name of the type."
I appreciate if you can help me.
The compiler thinks that you are declaring a member function not a member variable (see here for more info on initialization). The following compiles. I am using Index instead of size_t to get rid of some warnings (narrowing conversion). You can play around with the code here: https://godbolt.org/z/yV1NUL
#include <iostream>
#include <vector>
#include <Eigen/SparseCore>
#include <Eigen/Sparse>
using namespace Eigen;
namespace A {
class A
{
Index max_doc_id = 4;
Index max_term_id = 4;
SparseMatrix<float, Eigen::RowMajor, int64_t> smat{max_term_id, max_doc_id};
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
}
Note: you should not use using namespace in header files, see "using namespace" in c++ headers
Edit: please also consider what user #ggael says in the comments, most likely you do not need EIGEN_MAKE_ALIGNED_OPERATOR_NEW because the SparseMatrix is not fixed-size vectorizable
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.
How do I map a key to a native data type like structure?
I wrote this snipped but I couldn't compile it. Do you have any ideas on how to fix it?
#include <map>
#include <iostream>
typedef struct _list
{
int a,b;
}list;
map<int,list> test_map;
int main(void)
{
cout <<"Testing"<< endl;
}
map resides in the std:: namespace. Two possible ways to fix this:
using namespace std;
// ...
map<int, list> test_map;
or
std::map<int, list> test_map;
I prefer the second method, but it's a purely personal choice.
On a related note, there is no real limitation on what you can put in a map, aside from the fact that they must be copyable/assignable, and that the key type must have a < operator (or you can also provide a comparer functor).
EDIT: Seems like <list> is included somewhere, either in <iostream> (unlikely) or <map> (strange but not impossible). A using namespace std will cause std::list to clash with your own struct. The solution: rename your struct, or remove the using namespace and put std:: where it's needed.
Added std where required.
Renamed list to mylist to avoid clash with std::list. Avoid typenames and variable names that clash with common usage.
Now compiles OK in VS2008.
#include <map>
#include <iostream>
typedef struct _list
{
int a,b;
} mylist;
std::map<int,mylist> test_map;
int main(void)
{
std::cout <<"Testing"<< std::endl;
return 0;
}
There's no issue with using your struct in the STL containers provided it's copyable cleanly (copy constructor), assignable (implements operator=) and comparable (implements operator<).
A number of problems here:
You're missing either a using::std or std::map, so the compiler doesn't know what map<int,list> means.
Assuming you have a using namespace std declaration, your typedef list might collide with the STL collection of the same name. Change the name.
Your typedef struct _tag {...} tag; construct is an archaic holdover from the 80's. It is not necesarry, and frankly rather silly. It gets you nothing.
Here's your code fixed:
#include <map>
#include <iostream>
struct MyList
{
int a,b;
};
std::map<int,MyList> test_map;
int main(void)
{
std::cout <<"Testing"<< std::endl;
return 0;
}
map<int, _list> test_map; or don't use list(much better) as a name of structure. (You probably also have
#include <list>
...
using namespace std;
somewhere in your code.
I would try to avoid using codepad at all.
I have done a couple of tests with your code and it seems that
it is adding an implicit (and unwanted) using namespace std --it does not require you to qualify map, cout or endl.
it is (probably) including more standard headers than you might want, including #include <list>.
That means that when the compiler looks at the code it is seeing two list, your version and the one in std. Because of the using directive, both are in scope in the line where you create the map and the compiler is not able to determine which to use.
Two simple things that you can do: change the name of your type for the simple test to something other than list (ouch! the tool forcing your naming choices!) or fully qualify the use:
#include <map>
struct list {
int a,b;
};
std::map< int, ::list > the_map;
// ...
Note that codepad is adding the include by itself and the using directive, so it will also compile:
struct list {
int a,b;
};
map<int,::list> the_map;
But that piece of code is wrong
You seem to be comming from C. Try this:
#include <map>
#include <iostream>
struct list
{
int a,b;
};
std::map<int,list> test_map;
int main(void)
{
std::cout <<"Testing"<< std::endl;
return 0;
}
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>