queries with string and vector libs

This is a discussion on queries with string and vector libs within the C++ Programming forums, part of the General Programming Boards category; hi there, first post etc, bit of a c++ newb but then i guess everyone was at one point, eh? ...

  1. #1
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20

    queries with string and vector libs

    hi there, first post etc, bit of a c++ newb but then i guess everyone was at one point, eh?

    anyway, im not so sure on the use of the two libs "string" and "vector". ive searched on google but finding an application of each of them is more difficult than i expected. im not neccesarily after code, just explaining the purposes of the vector lib and what it does, and why the string lib exists (why cant i just use char * mystring etc)?

    cheers for any help

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    The C++ string class is there to encapsulate a string and provide safe, easy, and efficient use of strings in your program. You can just use char* in your program, but the string class is a higher level tool that handles low-level issues like memory management which makes it easier and safer to use.

    The C++ vector class provides the same thing for C style arrays. It handles memory management for you, so you don't have to worry about it (as much).

    If you are learning C++, you should learn and use these types of classes instead of their C counterparts.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    why cant i just use char * mystring etc)
    You can, but the string class is easier to use. Here is an example:
    Code:
    string str1 = "hello";
    string str2 = " world";
    
    string str3 = str1 + str2;
    
    string str4 = str1.substr(0, 2); 
    cout<<str4<<endl; //he
    
    int position = str1.find('l');
    cout<<position<<endl<<endl; //2
    As for vectors, they can hold any type and their size expands automatically to accomodate whatever you put in them:
    Code:
    vector<int> ints1;
    for(int i = 0; i < 10; i++)
    {
    	ints1.push_back(i);
    }
    
    for(int j = 0; j < 10; j++)
    {
    	cout<<ints1[j]<<" ";
    }
    cout<<endl;
    
    ints1.erase(ints1.begin() + 3); //erases 3rd element from the beginning
    
    //display the vector using an "iterator"
    //which is like a pointer that moves along the vector:
    for(vector<int>::iterator k = ints1.begin(); k != ints1.end(); k++) 
    {
    	cout<<*k<<" ";
    }
    cout<<endl;
    
    //vector of strings:
    vector<string> strings;
    strings.push_back("hello");
    strings.push_back("world");
    ive searched on google but finding an application of each of them is more difficult than i expected
    There should be hundreds of examples. Search "C++ vectors". Here is one:

    http://www.csci.csusb.edu/dick/cs201/vectors.html

    You might find a <map> useful, which is an "associative array", which means the index values aren't the integers 0, 1, 2.... Instead the index values are strings(or any other type you want them to be):
    Code:
    //include <map> 
    map<string, string> phoneNumbers; //<type of index value, type of value>
    phoneNumbers["John"] = "897-4567";
    phoneNumbers["Sally"] = "123-5678";
    cout<<phoneNumbers["Sally"]<<endl;
    Last edited by 7stud; 11-07-2005 at 11:55 AM.

  4. #4
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20
    cheers guys! didnt expect to get one reply so quickly let alone two =)

    the strings lib looks excellent, i was toying round with some OOP and inheritance the other day and was having some trouble with concatenating strings (was trying to pick a random first name and random surname and mix them... had some difficulties). was also having problem assigning a string to an array of chars as they were different sizes, etc.

    vector classes are dynamic arrays then? i assume they can hold classes?

    very useful stuff, cheers.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> vector classes are dynamic arrays then? i assume they can hold classes?

    Yes, they can hold classes just like arrays can.

    One note: In general, your classes should always have valid copy behavior, but this is especially true when using them with vectors (since a vector stores a copy of the class you add to it). If your class holds only plain datatypes like int or double, or if it holds other classes with valid copyablility like string, and it doesn't have any array or pointer data members, then it should automatically be safe to copy. Otherwise, you need a copy constructor and copy assignment operator to override the default versions and copy the data correctly.

  6. #6
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20
    okay i can see why that arrays or pointers would need something else to copy them (memory allocation etc), and i understand constructors/destructors, but what is the syntax for defining a copy constructor etc?

    must appologise for the lame questions, i got visual studio.net 2003 through microsoft for free because my uni has signed up for it (i can get xp professional etc too), but it came with no help files so i cant search them.

    thanks again, this has been so much help.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    You can search the web (MSDN is the helpfile for visual studio, and it is available online). You can also search the board.

    The normal copy constructor prototype is (where T is the name of your class):
    Code:
    T(const T&);
    Assignment operator:
    Code:
    T& operator=(const T&);
    The first doesn't return anything (it is a constructor). The second should return *this;.

  8. #8
    Registered User combatdave's Avatar
    Join Date
    Nov 2005
    Posts
    20
    ah cheers, didnt know that was what the msdn was. thanks for the pointers, have a good night mate.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    I would recommend against the MSDN for beginners, the text is often so convoluted and dense that it skips over the stuff that is obvious to more experienced programmers. (not that the MSDN is a bad reference).

    For container classes etc, I find http://www.sgi.com/tech/stl/table_of_contents.html
    to be a more useful reference, its simpler and i think as thorough. Anyway, just though i might add another place for you to look for info.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21