C++ std::array don't work on my local compiler - c++

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;

Related

Error: 'array': ambiguous symbol

I'm tring to use boost array
but I got this error:
error: 'array': ambiguous symbol
here is my code:
#include <iostream>
#include <boost/array.hpp>
#include <boost/regex.hpp>
using namespace boost;
using namespace std;
int main(int argc, char* argv[])
{
array<int, 10> a{3};
cout << "a[0]= " << a[0];
return 0;
}
This error arise when I include the boost library
any idea ?
boost::array and std::array are not ambiguous because they are scoped by different namespaces.
When you do:
using namespace boost;
using namespace std;
You tell the compiler to look symbols up in both these namespaces. But the compiler now doesn't know which array you're talking about when you just type array. It could be boost::array or it could be std::array thus it is ambiguous.
You could fix this by either removing the using namespace std; or by specifying that you're using the Boost version by using boost::array instead of just array.
Incidentally I understand trying to learn Boost functionality, but you shouldn't bother learning boost::array, right in the Introduction to boost::array it tells you:
std::array is (as of C++11) part of the C++ standard. The differences between boost::array and std::array are minimal. If you are using C++11, you should consider using std::array instead of boost::array.
add core:: before the array
so core::array

(Software issue) Can anyone suggest a download that will definitely bring my Mingw32 compiler up to speed?

#include <iostream>
#include <string>
using namespace std;
int main(){
string s = "wassup", newVal = "though";
cout << *(s.insert(s.begin(), newVal.begin(), newVal.end()));
}
This brings up the problem that the return type of string's insert member function is void (error: void value not ignored as it ought to be). This link indicates C++98 returns void but the "new" standard C++11 does indeed return an iterator.
A bit of context, I actually faced this problem earlier. I was/am using CodeBlocks (GCC Compiler Collection) on Windows 7 64-bit and this program gave the same issue (dereferencing void):
#include <iostream>
#include <list>
int main(){
list<int> x = {1,2,3,4};
*(x.insert(++x.begin(), 3, 2));
for(auto c : x)
cout << c;
}
I posted my issue on a different forum and a user pointed out Mingw32 was missing that particular C++11 change, indicating Mingw-w64 does not have this issue. So I went straight to installing Mingw-w64 on Code::Blocks using this guide and the problem was resolved. It's only now I've found out that the overloaded function which takes three iterator parameters STILL returns void.
I'm a little confused as to why Code::Blocks Mingw32 didn't supply a fully updated C++11 standard. Can anyone suggest a download that will definitely bring my compiler up to speed?

c++ how to make vector store bitsets?

So basically I want to store std::bitset<128> within std::vector<>.
I have tried this:
std::vector<std::bitset<128>> myVector;
But compiler complains about invalid template parameters. How can I fix this and can I add this type into a typedef for later use?
EDIT: Indeed my compiler seems to use C++03 as its default standard and I had to use vector<bitset<128> > for this to work with current settings.
Yes it works, even with a typedef.
But note that you need c++11 (-std=c++11) to use two right adjacent angle brackets in your declaration.
#include <vector>
#include <bitset>
int main() {
typedef std::vector<std::bitset<128>> Bitset_vec;
Bitset_vec v;
return 0;
}
If you don't have support for c++11, add a space between the brackets :
typedef std::vector<std::bitset<128> > Bitset_vec;
Live example here (note the -std=c++11 option)

Why a variable length array compiles in this c++ program?

It is said that arrays are allocated at compile time, then the size must be const and available at compile time.
But the following example also works, Why?
#include <iostream>
#include <vector>
using namespace::std;
int main()
{
vector<int> ivec;
int k;
while(cin>>k)
ivec.push_back(k);
int iarr[ivec.size()];
for (size_t k=0;k<ivec.size();k++)
{
iarr[k]=ivec[k];
cout<<iarr[k]<<endl;
}
return 0;
}
Compile your code with -pedantic.
Most compilers support variable length arrays through compiler extensions.
The code works due to the compiler extensions, However as you noted the code is non standard conforming and hence non portable.

C++ array Visual Studio 2010 vs Bloodshed Dev-C++ 4.9.9.2

This code compiles fine in Bloodshed Dev-C++ 4.9.9.2, but in Visual Studio 2010 I get an error: expression must have a constant value. How do I make an array after the user input about array size without using pointers?
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int size = 1;
cout << "Input array size ";
cin >> size;
int array1[size];
system("PAUSE");
return 0;
}
Use an std::vector instead of an array (usually a good idea anyway):
std::vector<int> array1(size);
In case you care, the difference you're seeing isn't from Dev-C++ itself, it's from gcc/g++. What you're using is a non-standard extension to C++ that g++ happens to implement, but VC++ doesn't.
The ability to size automatic arrays using a variable is part of C, not part of C++, and is an extension that GCC seems to want to foist on us all. And DevC++ is an unholy piece of cr*p, although it is not at fault here. for a change (this is entirely GCC's doing) - I can't imagine why you (or anyone else) would want to use it.
You should really compile your C++ code with GCC with flags that warn you about stuff like this. I suggest -Wall and -pedantic as a minimum.
Or
int array1 = new int[size];
will work aswell I believe (been a month or 3 since I last touched C++)
But indeed, if using C++, go for an std::vector, much more flexible.