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;