-6

So I found this code on the internet, but as I'm not that familiar with C++. I found difficult to understand this: how does a vector suddenly becomes a matrix?

int main(){

int n;
string v[MAX];

cin >> n; 

for(int i=0;i<n;i++) 
 cin >> v[i];

for(int i=0;i<n-1;i++){
    int y1,y2;
    y1=v[i].size();
    y2=v[i+1].size();
    for(int j=0; j<y1 && j<y2 ;j++)
        if(v[i][j]!=v[i+1][j]){  // here <-
            int x1,x2;
            x1=(int) v[i][j]-'A';
            x2=(int) v[i+1][j] - 'A';
            m[x1][0]=true; 
            m[x2][0]=true;
            m[x1][x2+1]=true; 
            break;
        }
 }
Evan Carslake
  • 2,267
  • 15
  • 38
  • 56
MSilva
  • 1

4 Answers4

4
string v[MAX];

is an array of std::string (presumably - this is one reason to avoid using namespace std;. How do I know what type of string it is?).

You can access elements of an array with []:

int someInts[5];
someInts[3]=1000; // sets the 4th int (counting starts from 0)

You can also access characters in a std::string with []:

std::string name("chris");
std::cout << name[3]; // prints 'i'

So you can access the letters in an array of std::strings with two sets of []:

std::string names[10]; // 10 names
names[3] = "chris"; // set the 4th name
std::cout << names[3][1]; // prints 'h'
//                    ^ access letter in string
//                 ^ access string in array
Community
  • 1
  • 1
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
1

Here is a self-explanatory example

int main()
{
  std::string name;
  name = "test";
  for(int i = 0; i<4; i++)
      std::cout<<name[i]<<std::endl;
  std::cout << "Hello, " << name << "!\n";
}

It will print

t
e
s
t
Hello, test!

So, an array of strings is actually a 2D array of characters, that you called a matrix.

yildirimyigit
  • 3,013
  • 4
  • 25
  • 35
0

string v[N] is an array of string, string itself is an array of chars.

tchelidze
  • 8,050
  • 1
  • 29
  • 49
  • "`string` itself is an array of `char`s" - I understand you are trying to simplify for a beginner, but I think this goes too far, to cause confusion later. – BoBTFish Jan 09 '16 at 17:08
  • note that It is not simplification for beginners, `string` internally is implemented with `char[]` (character array). – tchelidze Jan 09 '16 at 17:10
  • So if I create any class containing an array, I can access that array by using `[]` on the surrounding class? No. That's just some internal detail. `string` is a `string`, that *behaves* a bit like an array of `char`. (And contains a *pointer to* an allocated array of `char`). – BoBTFish Jan 09 '16 at 17:14
  • Yes, you can if you `overload` index `operator` as it is done in `string` – tchelidze Jan 09 '16 at 17:19
  • **Exactly!** *That* is what makes `[]` work on a `std::string`. Calling a `std::string` an array of `char`s completely avoids mentioning this, which is what actually matters. "`std::string` is designed to be able to sometimes treat it like an array of `char`s" is a much better description. – BoBTFish Jan 09 '16 at 17:20
0

Since, as the commentor pointed out, there are neither vectors or matrices in the code you gave, I'll make a couple assumptions:

  • By "vector", you mean "array"
  • You think that double square brace operators ([][]) indicate a matrix.

If those are both true, I can explain what you're seeing:

string[5] strings = { Some Strings... } 

//The first string in the array
string string1 = strings[0];
//The first letter of the first string
char char1 = string1[0];

//The above is the same as:
char char1Again = strings[0][0];

In the line above, the first square bracket operator returns the first string in the array. The second square bracket operator is then applied to that string, which returns the first character of that string.

This works because both arrays and Strings (which are really arrays themselves deep down) implement the square bracket operator to access their internal elements by index.

Technically, in a convoluted way, you could call this a matrix, but "2D array of characters" would be more appropriate.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117