Why the following code doesn't compile,
#include <vector>
using std::vector;
vector<int> v; // Error: too few template arguments, expected 2
but the same code with map (and pair, set, ...) instead of vector works?
#include <map>
using std::map;
map<int, int> m; // OK
And also this code works fine:
#include <vector>
using namespace std;
vector<int> v; // OK
I know that constructor of std::vector has two arguments (type and allocator), but why vector behaviour is so different from other containers?
UPD: I'm sorry, this is my mistake. Actually the code does compile, but CLion marks it as an error. So it is CLion's bug.
It is yet not fixed CLion bug: https://youtrack.jetbrains.com/issue/CPP-5758#u=1454575544687.
As a workaround you can try to use libstdc++ instead of libc++, see https://youtrack.jetbrains.com/issue/CPP-5758#comment=27-2389700.
Related
I have just configured c/c++ in netbeans on ubuntu
and when i try to use std::pair it seems that the compiler cannot find it
that is very strange
the default version of c++ is c++11
that a slice of my code
int n, k;
cin >> n>>k;
vector<pair<int,int> > x(n);
thanks in advance
You need to include the right header files a the beginning of your source files so that the compiler know the different types/objects:
#include <iostream> // For std::cin
#include <vector> // For std::vector
#include <utility> // For std::pair
And use the std namespace by default if you want (before main() typically):
using namespace std;
I follow this example array::size, but it's don't work on my Dev C++ IDE, and I don't know why.
I tried to run on Code Blocks but it's still the same.
I think code is correct but how to fix this problem
here is my code
#include <iostream>
#include <array>
using namespace std;
void useArray()
{
array<int,4> myInts;
myInts[0]=1;
cout<<"Size of Array: "<<myInts.size();
}
int main()
{
useArray();
system("pause");
}
And this is compiler error:
std::array is C++11 only. Dev-C++ does not support C++11.
If you're just looking for options to Visual Studio this article is the right starting place: http://www.cplusplus.com/articles/36vU7k9E/
In the case that you're using a compiler that does not support C++11 (using the flag: -std=c++11) you always have the chance to use a Boost alternative.
Boost comes with the Boost Array Library which is an STL conform Array. The generated code will be similar to a C Array while having the opportinity to use additionnal methods on it e.g determinate the array size. All this basically works as the C++11 version of an Array.
It lives in the 'boost' namespace instead of 'std' therefore you need to include the following header to use it:
#include <boost/array.hpp>
You should then set a typedef declaration for the Boost Array:
typedef boost::array<std::string, 3> array;
// The 3 stands for the number of elements the array can hold
Implementing one of these Arrays is then fairly simple:
array a;
I am learning about STL currently and I am trying to implement an unordered map for a dictionary file.
This is the first time I have done this so I did a lot of research prior to trying this.
I want to do an unordered map for my assignment because we can receive extra points if we can make our project faster than what our professor's solution currently is.
The problem I am running into is that I keep getting this error:
SpellCheck.h:16: error: ISO C++ forbids declaration of âunordered_mapâ with no type
I am sure my syntax is correct, but I could be missing something.
I am not sure if this helps, but I am compiling on a school server using g++.
My g++ version is g++ (GCC) 4.4.7 .
#ifndef SPELLCHECK_H
#define SPELLCHECK_H
#include <vector>
#include <tr1/unordered_map>
#include <string>
using std::vector;
using std::string;
class SpellCheck
{
private:
typedef vector<string> Vector;
typedef unordered_map<string, int> Dictionary;
};
#endif
This should also work. Compile with -std=c++0x flag to use c++11 with g++.
#ifndef SPELLCHECK_H
#define SPELLCHECK_H
#include <vector>
#include <unordered_map>
#include <string>
class SpellCheck
{
private:
typedef std::vector<std::string> Vector;
typedef std::unordered_map<std::string, int> Dictionary;
};
#endif
I'd like to ask you how could I copy all second elements from
map<string, string> myMap
to
deque<string> myDeq
using for_each or transform without creating a functor. I tried it like in this question
transform(myMap.begin(), myMap.end(), back_inserter(myDeq), mem_fun_ref(&map<string, string>::value_type::second));
but it didn't work for me - I got error "Illegal use of this type".
The reason you get the error is because map<string, string>::value_type::second is not a member function. It is just a member variable of the std::pair template struct.
One possible solution without using functors is with the use of lambdas. But it's a C++11 feature so I don't know if that's what you want.
Take a look at the following example
#include <iostream>
#include <map>
#include <deque>
#include <algorithm>
#include <string>
#include <iterator>
using namespace std;
int main()
{
map<string,string> myMap;
deque<string> myDeque;
myMap["key1"]="value1";
myMap["key2"]="value2";
transform(myMap.begin(),myMap.end(),back_inserter(myDeque),[](map<string,string>::value_type p){return p.second;});
copy(myDeque.begin(),myDeque.end(),ostream_iterator<string>(cout,"\n"));
}
I am using Netbeans 7.1 on Ubuntu 11.04.
The following call
set< Triangle > V;
gives the error message
error: ‘set’ was not declared in this scope
and the following call
vector< Triangle > ans;
gives the error message
error: ‘vector’ was not declared in this scope
This despite my having
#include <vector>
#include <set>
#include <map>
at the beginning of the C++ file.
At help resolving this would be greatly appreciated.
Peter.
Vectors Sets and map are part of the c++ Standard Library so you need to call vector/set/map with
std::vector< Triangle > ans;
or add
using namespace std;
after the include statements.
you forgot about namespace std :
std::set< Triangle > V;
std::vector< Triangle > V;
They live in the std namespace. So, either fully quality the types (std::vector) or use a using statement (using namespace std;).
The latter option pollutes the global namespace. Never do that in a header file (otherwise the entire namespace is imported when you include the header) and only do it in your implementation file if you know that it isn't going to cause any collisions.
#include <vector>
int main(...) {
vector v; // no worky
std::vector v; // ok!
}