1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to store CString using Vector in C++

Discussion in 'C++' started by haroonrulz, Sep 7, 2012.

  1. haroonrulz

    haroonrulz Banned

    Joined:
    Aug 30, 2012
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Dear Gurus/Experts in C++,

    I am new to C++.My Question is very basic.I need CString to be stored in Vector, as I am new even after several attempts I dont know how to do it.Kindly help me.My code is as below:


    void Editcontrol(CString prop1,CString prop2,CString prop3)
    {
    Vector <string> v;

    // I need to store CString in Vector, even after many tries I could not able to do it
    }


    int main()
    {
    Editcontrol("Haroon","Rizwan","Noor");

    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    If you want a vector of CStrings then the definition would be vector<CString>. vector<string> will give you a vector of strings, not CStrings.

    C++ is case sensitive so vector and Vector are not the same thing.

    This is only a guess, because you didn't say why you couldn't do it. I'm guessing you got some sort of error, would that be correct?
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Here's some code I just wrote:
    Code:
    void test51()
    {
    	vector<string> v;
    	v.push_back("Hello");
    	v.push_back("World");
    }
    
     
  4. Becks

    Becks New Member

    Joined:
    Feb 23, 2013
    Messages:
    2
    Likes Received:
    1
    Trophy Points:
    0
    Here is an example how to store a textfile (each line) in a vector.
    Code:
    #include <vector>
    #include <fstream>
    
    using namespace std;
    
            ifstream myfile(path.c_str());
    	char zeile [500];
    	if (myfile)
    	{
    		int b=0;
    		while(myfile.getline(zeile, 500))
    		{
    			line.push_back(zeile);
    
    
    		}
    
    		myfile.close();
    }
    
    path is just a string which stores the path to the txt-file
     
    shabbir likes this.

Share This Page