Reputation: 288550
I wrote some JavaScript code but now I want to translate it to C++ because of perfomance, but my knowledge of C++ is low.
In JavaScript I can do:
var arr=[[['à','á'],['è','é'],['ì','í'],['ò','ó'],['ù','ú']],['a','e','i','o','u']];
How can I do something like that in C++? Must I do
vector<vector<char>> arr;
and then set each element manually?
arr[0][0]='à';
arr[0][1]='á';
...
Upvotes: 2
Views: 433
Reputation: 4626
Alternatively, you could use std::vector< std::pair< char, char > >
.
Note, that you cannot use operator[]
(ie. call arr[0]
or arr[0][0]
on an empty vector (since the requested elements are not there, you access invalid memory which (hopefully) will crash your program). Add elements to a vector with push_back(...)
:
vector< pair< char, char > > arr;
arr.push_back( make_pair( 'à','á' ) );
arr.push_back( make_pair( 'è','é' ) );
arr.push_back( make_pair( 'ì','í' ) );
arr.push_back( make_pair( 'ò','ó' ) );
arr.push_back( make_pair( 'ò','ó' ) );
arr.push_back( make_pair( 'ù','ú' ) );
Depending on what you want to do, std::map< char, char >
might be useful for you as well. It will allow you to access elements using the first type as a key, eg.
std::map<char, char > mymap;
mymap['à'] = 'á';
// will print "Element à = à"
cout << "Element à = " << mymap['à'] << endl;
Upvotes: 2
Reputation: 264621
This will work for C++11
std::vector<std::vector<char>> var arr={{'à','á'},{'è','é'},{'ì','í'},{'ò','ó'},{'ù','ú'},{'a','e','i','o','u'}};
// Note it is changed from the original I removed one level of nesting from part of the array.
If you are using C++03.
Then you need to change things slightly.
std::string var[] = {"àá'", "èé", "ìí", "òó", "ùú", "aeiou"};
The types have changes but it has the same affect and access to the object var
is unchanged.
Upvotes: 2
Reputation: 5494
Here's a code snippet using c++0x:
#include <vector>
#include <iostream>
using namespace std;
int main(){
vector< vector<char> > v = { {'a','b','c'}, {'d','e','f'} };
for (unsigned i=0; i<v.size(); i++){
cout <<"Subvector: ";
for (unsigned j=0; j<v[i].size(); j++)
cout << v[i][j];
cout << endl;
}
return 0;
}
Compile it with g++ -std=c++0x
Upvotes: 0
Reputation: 61970
Yes, it's at least a bit more work unless you have C++11, which adds the possibility of constructors taking an initializer list:
std::vector<std::vector<char>> arr {{'a', 'b'}, {'c', 'd', 'e'}, {'f'}};
You might also consider replacing std::vector<char>
with std::string
.
Upvotes: 7
Reputation: 29966
Unfortunately you can't initialize a vector as an array. However, you can initialize a vector with an array. For a single vector:
static const int arr[] = {1,2,3,4};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
Initializing a vector of vectors is a little tricky, but you can do it in a cycle either like above, or like this: (pseudocode):
for( int i =0; i < something; i++ )//the outer vector
{
vector.pushBack( std::vector<int> );
for( int j =0; j < somethingElse; j++)
vector[i].push_back(j);
}
Upvotes: 1