Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 23rd, 2012  |  syntax: None  |  size: 0.89 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. list of char* to vector of strings
  2. //write a program to assign the elements from a list of char* pointers to c-style character strings to a vector of strings
  3. #include <iostream>
  4. #include <cstring>
  5. #include <vector>
  6. #include <list>
  7. #include <string>
  8. using namespace std;
  9. int main ()
  10. {
  11.     list<const char*> clist;
  12.     cout<<"please enter a string"<<endl;
  13.     for(string s; getline(cin,s); )
  14.     {  
  15.         const char* cp=s.c_str();
  16.         clist.push_back(cp);
  17.         cout<<*cp;
  18.     }
  19.     cout<<*clist.begin();
  20.     vector<string> svec;
  21.     svec.assign(clist.begin(),clist.end());
  22.     for(vector<string>::iterator iter=svec.begin(); iter!=svec.end(); ++iter)
  23.         cout<<*iter<<endl;
  24. return 0;
  25. }
  26.        
  27. cout << cp;  // You're providing cout a const char *
  28.        
  29. cout << *cp; // You're providing cout a char
  30.        
  31. cout<<*cp;
  32.        
  33. cout<<cp;
  34.        
  35. cout << cp << endl;
  36.        
  37. const char* cp=s.c_str();
  38.        
  39. cout<<*cp;