Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Accessing TCHAR[] array from an vector.

Accessing TCHAR[] array from an vector.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++graphicsdata-structures
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Comp_Users
    wrote on last edited by
    #1

    Hi, In my application, I store an array of TCHAR[MAX_PATH] strings into an vector as indicated by the code snippet below.

    //in the .h file
    #include <vector>
    typedef std::vector<std::basic_string<TCHAR>> StringVector;

    //In the .cpp file

    BOOL CMyApp::AddStringToVector()
    {
    StringVector vect;

    vect.push\_back(\_T("One"));
    
    vect.push\_back(\_T("two"));
    
    vect.push\_back(\_T("three"));
    
    //later on try accessing the vector element.
    
    TCHAR szResult\[MAX\_PATH\]={0};
    \_tcscpy(szResult, vect\[1\]);
    

    }

    I am able to add all the string. But accessing the stored string from the vector is the problem. _tcscpy(szResult, vect[1]); gives an compilation error Error 11 error C2664: 'wcscpy' : cannot convert parameter 2 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const wchar_t *' I think this error is caused because the TCHAR that is stored in the vector as std::basic_string<_Elem,_Traits,_Ax> and that the compiler is not able to convert it to an TCHAR[MAX_PATH]. How do I go about accessing the TCHAR elements in the vector? Please help.

    C 1 Reply Last reply
    0
    • C Comp_Users

      Hi, In my application, I store an array of TCHAR[MAX_PATH] strings into an vector as indicated by the code snippet below.

      //in the .h file
      #include <vector>
      typedef std::vector<std::basic_string<TCHAR>> StringVector;

      //In the .cpp file

      BOOL CMyApp::AddStringToVector()
      {
      StringVector vect;

      vect.push\_back(\_T("One"));
      
      vect.push\_back(\_T("two"));
      
      vect.push\_back(\_T("three"));
      
      //later on try accessing the vector element.
      
      TCHAR szResult\[MAX\_PATH\]={0};
      \_tcscpy(szResult, vect\[1\]);
      

      }

      I am able to add all the string. But accessing the stored string from the vector is the problem. _tcscpy(szResult, vect[1]); gives an compilation error Error 11 error C2664: 'wcscpy' : cannot convert parameter 2 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const wchar_t *' I think this error is caused because the TCHAR that is stored in the vector as std::basic_string<_Elem,_Traits,_Ax> and that the compiler is not able to convert it to an TCHAR[MAX_PATH]. How do I go about accessing the TCHAR elements in the vector? Please help.

      C Offline
      C Offline
      Comp_Users
      wrote on last edited by
      #2

      Got the solution. I should be using to access the TCHAR stored in the vector as given below.

      _tcscpy(szResult, vect[1].c_str());

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups