equivalent in C++ from VB?

Hi, I learned vb pretty well and now i am learning c++
can someone give me the syntax for the equivalent of the following code in VB?

Say I have an array of strings(2 Dimensional), and I want the value of the first character in a string located at the position (2,3) in the array.
In VB this would look like :

char1= ArrayName(2,3)(0)

the second character would be

char2=ArrayName(2,3)(1)

and so on..

what is the equivalent in c++?
Last edited on
It depends on what kind of container you use. Normally in C++ you would use std::vector, but there is no such thing as a 2D array in C++ - you can either have a vector of vectors (aka ragged array) or you can have a single vector and use math to treat it as 2D.
but there is no such thing as a 2D array in C++

C++ has two-dimensional arrays:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main()
{
	std::string arr[2][3] = { { "a1", "b2", "c3" }, { "d4", "e5", "f6" } };
	char ch1 = arr[1][2][0];
	char ch2 = arr[1][2][1];
	std::cout << ch1 << '\n' << ch2;
}

Output:
f
6
Well, technically, with std::string arr[2][3] , what we have is
an simple array of size 2: element_type arr[2] where element_type is the type of each element in the array.
In this case, the type element_type is an array of 3 std::string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
#include <string>

int main()
{
    using element_type = std::string[3] ;
    using array_type = element_type[2] ;
    array_type arr = { { "a1", "b2", "c3" }, { "d4", "e5", "f6" } };

    auto& row = arr[1] ; // type of 'row' is 'element_type'
    // auto& row = *( arr + 1 ) ;

    std::string s ;
    s = row[1] ;
    s = arr[1][1] ; // same as above
}


However, the terms 'two-dimensional array' and 'multi-dimensional array' are widely used and widely understood.

The IS:
In a declaration T D where D has the form
D1 [ constant-expression opt ] ...
... then the type of the identifier of D is an array type;

When several “array of” specifications are adjacent, a multidimensional array is created ...
[Example:
...
static int x3d[3][5][7];
declares a static three-dimensional array of integers, with rank 3×5×7. In complete detail, x3d is an array of three items; each item is an array of five arrays; each of the latter arrays is an array of seven integers.
Any of the expressions x3d, x3d[i], x3d[i][j], x3d[i][j][k] can reasonably appear in an expression. ...
—end example]
...
A consistent rule is followed for multidimensional arrays. If E is an n-dimensional array of rank i×j×...×k,
then E appearing in an expression that is subject to the array-to-pointer conversion is converted to a pointer to an (n−1)-dimensional array with rank j×...×k. ...
...
[Example: consider
int x[3][5];
Here x is a 3 × 5 array of integers. When x appears in an expression, it is converted to a pointer to (the
first of three) five-membered arrays of integers. ...
—end example ] ...
[Note: It follows from all this that arrays in C ++ are stored row-wise (last subscript varies fastest) and that
the first subscript in the declaration helps determine the amount of storage consumed by an array but plays
no other part in subscript calculations. —end note ]
...
template <class T> struct rank;
If T names an array type, an integer value representing the number of dimensions of T; otherwise, 0.
std::rank: http://en.cppreference.com/w/cpp/types/rank
Topic archived. No new replies allowed.