dimanche 14 octobre 2018

Why there is so much gap in address of two vector elements?

I'm learning about vectors in c++. On printing address of two adjacent vector<string> elements I'm getting result which is confusing me a little bit.
Program

#include<iostream>
#include<vector>
using namespace std;

int main()
 {  
   vector<string> word;

   //Enter any two words
     string ch;
     for(int i=1;i<3;i++)
      {
       getline(cin,ch);
       word.push_back(ch);
       }

   //Display the words
     for(auto i:word)
      {
       cout<<i<<" ";
      }  
     cout<<endl;

    //Display address of first two elements

      cout<<word.data()<<" "<<(word.data()+1)<<endl;

     return 0;
  }

INPUT Data: hello
world
OUTPUT : hello world
0x1b9f060 0x1b9f080

What I'm not getting is that there are only 10 character in total(5 in first input and another five in second)and each character take 1 byte space.
So why there is gap of 20 bytes between both vector elements?
I checked with vector<int> but the gap was only of four bytes.

Aucun commentaire:

Enregistrer un commentaire