Simplest way to initialize vectors

Hi everyone,

I’ve started to learn c++/oF very recently and I’m exploring the use of vectors and arrays. I’m working on a program to help me learn the most important things I need to know for my upcoming projects. I have a relatively large number of pre-defined values that are used by several functions to filter media. I’ve only ever worked with JavaScript before, so it seemed most familiar to put these values into arrays like these

[tt]int probContent[4][4] = {{16,18,19,19},{0,35,42,46},{3,8,21,23},{0,4,6,9}};
int probPeople[3][3] = {{34,40,45},{4,4,6},{8,8,47}};
string qualContent[4] = {“set-up”, “performance”, “kate”, “still”};
string qualPeople[3] = {“0”, “1”, “2”};[/tt]

I soon realized that for my needs it’s a bit tedious to work with arrays and the general recommendation seems to be to use vectors anyway. However, I’m a bit confused about initializing vectors. I tried a few things described in this StackOverflow thread, and this-one, but they don’t seem to work.

The ideal solution I was hoping to find is obviously

[tt]vector s{“s1”, “s2”, “s3”};[/tt]

but I only get errors with this. I’m working with XCode 4.6.2 btw and was under the impression that these newer c++11 features would work. As a reminder I’m very new to all this and only just found out that there is such a thing as c++11 :slight_smile: so here one of my questions: am I doing something wrong here or is this specific to oF?

The only way I have been able to initialize vectors so far was by basically having a structure like this:

[tt]string qualContent[4] = {“set-up”, “performance”, “kate”, “still”};
vector qualContentV;
for(auto & i : qualContent) qualContentV.push_back(i);[/tt]

… which seems a bit awkward, especially when I add the initialization for 2-dimensional arrays (which I haven’t attempted yet).

What would be the best way to initialize 1- and 2-dimensional vectors?

Hello,

syntax is :

  
  
vector<string> s("s1", "s2", "s3");  
  

or

  
  
vector<string> s = {"s1", "s2", "s3"};  
  

Beware, the last one seems only available in C++11 so it depends on which compiler you are working with.

Thanks for your reply.

I tried both of them and I’m getting errors.

  
vector<string> s("s1", "s2", "s3");  

“Expected parameter declarator”

  
vector<string> s = {"s1", "s2", "s3"};  

“Non-aggregate type ‘vector’ cannot be initialized with an initializer list”

So after a bit more research, I came to the conclusion that initializer lists only seem to work with c++11. I assumed that with the newest versions of OSX and XCode compiling for c++11 would be enabled, as I’ve only found relatively old links to this issue, but that’s not the case.

I changed

‘Compiler for C/C++/Objective-C’ to ‘Default compiler (Apple LLVM compiler 4.2)’,
‘C++ Language Dialect’ to ‘C++11 [-std=c++11]’ and
‘C++ Standard Library’ to ‘libc++ (LLVM C++ standard library with C++11 support)’

Finally works!