Converting to C-style strings. : vector subscript indexer « vector « C++ Tutorial






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

int main()
{
   string s( "STRINGS" );
   const char *ptr1 = 0;
   int len = s.length();
   char *ptr2 = new char[ len + 1 ]; // including null

   ptr1 = s.data();   
                      
   // copy characters out of string into allocated memory
   s.copy( ptr2, len, 0 );
   ptr2[ len ] = 0;  // add null terminator

   cout << s.c_str() << "\nptr1 is ";

   for ( int k = 0; k < len; ++k )
      cout << *( ptr1 + k );   // use pointer arithmetic

   cout << "\nptr2 is " << ptr2 << endl;
   delete [] ptr2;
   return 0;
}








16.2.vector subscript indexer
16.2.1.Use indexer to add elements to a vector
16.2.2.Use indexer to reference elements in a vector
16.2.3.Loop thourgh all elements in a vector using <
16.2.4.Loop through all elements in a vector using operator [] instead of operator *
16.2.5.Converting to C-style strings.