0

New to c++ (as of last night), but I've used c# a bit.

I'm trying to take a variable from a user and create an array of that size: the problem I'm running into is that c++ wants all arrays as constant sizes. I have no intention of trying to change the size of the array once its created, but i cant seem to even create it, because I haven't initialized it with a constant number.
This is the code I tried using:

int a;
cin >> a;
const int b = a;
int c[b];

I was attempting to get around the constant issue, but that doesn't work. The other option presented was vectors, but that seems inefficient (as they are comparable to lists in c#?). I don't need to be able to change the size of the array or anything similar. Am I missing something obvious here?

Thanks

Vitthal
  • 327
  • 1
  • 13
Mauvai
  • 461
  • 2
  • 5
  • 17
  • Unfortunately, C++ is complicated. There is *no way* you can learn it well and efficiently without [a book](http://stackoverflow.com/q/388242/1968), which will explain all the fundamentals, such as how to use containers. – Konrad Rudolph May 21 '14 at 10:53
  • I have a book, and it doesn't explain this properly (imo). this operation is very simple in c#, I'm just wondering is there an easy way around it – Mauvai May 21 '14 at 10:55
  • (See answer) If the book doesn’t explain this, it’s a bad book. Unfortunately, most C++ books are very bad (hence my link). That said, this is easily solvable in C++, it just requires some concepts, unfortunately. – Konrad Rudolph May 21 '14 at 10:56
  • A [std::vector<>](http://en.cppreference.com/w/cpp/container/vector) is far from terribly inefficient, and indeed the language you mentioned (C#) does pretty much the same thing under the hood without you knowing about it (just like almost everything else in C#, hiding most of this from the outside world). – WhozCraig May 21 '14 at 10:58

2 Answers2

4

Unfortunately, C++ is complicated. There is no way you can learn it well and efficiently without a book, which will explain all the fundamentals, such as how to use containers.

To answer your specific questions, std::vector is the way to go. It’s not inefficient. But yes, they are comparable to System.Collections.Generic.Lists. C++ does not currently have a non-resizable, variable-length container, but it couldn’t be implemented more efficiently than std::vector anyway.

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Correct. Efficiency is depending on how you use the container. In some cases a linked list `std::list` is prefereable to an array, sometimes a `std::deque`. However, they are a lot safer to use, compared to standard c arrays. – tillaert May 21 '14 at 10:57
  • Ok, say for a second i have a variable a= 5; how do i make a vector length 5 without adding 5 elements manually? or is adding the elements manually not inefficient? It was my understanding that every time you increase the size the whole thing needs to be moved to a new block of memory of the correct size – Mauvai May 21 '14 at 11:00
  • @user3660420 You can reserve the size of the vector when creating it. Also, whether the move is done, is dependent on your stl implementation. You still have the same problem when doing everything manually (with `new[]` and `delete[]`). And with stl containers, you have RAII, which makes cleanup much simpler. – tillaert May 21 '14 at 11:02
  • @user3660420 [In case you're wondering what **RAII** is](http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization). – WhozCraig May 21 '14 at 11:03
0

Try std::vector if you want to create monolith memory block of T objects. For setting size use "reserve" and "resize", difference you can watch at manual.

Or you can use operator new[], that allows to create dynamically array, but this way you will have to free memory, using operator delete[] after that.

Arkady
  • 2,084
  • 3
  • 27
  • 48