Reading
File: main1.cpp
#include "cmpslib19.h"
#include <vector>
#include <string>
int main()
{
/*
the stl::vector is a templated class
you can use it to store disparate types of data
*/
std::vector<std::string> weapons;
weapons.push_back( "Fist");
weapons.push_back( "Rocket Launcher");
weapons.push_back( "Shotgun");
weapons.push_back( "Chaingun");
weapons.push_back( "Chainsaw");
weapons.push_back( "Plasma gun");
weapons.push_back( "Super shotgun");
weapons.push_back( "BFG 9000");
std::vector<std::string>::iterator itr;
cout << "Loop through and print out the values\n";
for (itr = weapons.begin(); itr != weapons.end(); ++itr)
{
cout << "Value: " << *itr << endl;
}
// we can create as many vectors as we would like
std::vector<int> a_vector_of_ints;
std::vector<char> a_vector_of_chars;
std::vector<double> a_vector_of_doubles;
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------
File: main2.cpp
#include "cmpslib19.h"
#include <map>
#include <string>
int main()
{
/* a map is a container that holds "std::pairs" could be a piar of anything
it is a template class
it actually stores values that are pairs... another template class
*/
std::map<std::string,int> player_stats;
player_stats.insert( std::pair<std::string,int>("speed",9));
player_stats.insert( std::pair<std::string,int>("coordination",10));
player_stats.insert( std::pair<std::string,int>("power",11));
player_stats.insert( std::pair<std::string,int>("iq",8));
player_stats.insert( std::pair<std::string,int>("leadership",12));
player_stats.insert( std::pair<std::string,int>("luck",8));
player_stats.insert( std::pair<std::string,int>("piety",13));
player_stats.insert( std::pair<std::string,int>("lore",15));
std::map<std::string, int>::iterator itr;
cout << "Loop through and print out the values\n";
for (itr = player_stats.begin(); itr != player_stats.end(); ++itr)
{
cout << "Attribute: " << itr->first
<< " value " << itr->second << endl;
}
cout << endl;
// pretty cool it works like an array but with a string index
cout << "Whats my luck " << player_stats["luck"] << endl;
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------
File: main3.cpp
#include "cmpslib19.h"
/*
here is a very simple template class example
it will allow you to group 3 values together
into one object
*/
template <class T>
class Triple
{
private:
T val1;
T val2;
T val3;
public:
Triple (T one,T two,T three){val1=one;val2=two;val3=three;}
T GetVal1(){return val1;}
T GetVal2(){return val2;}
T GetVal3(){return val3;}
string ToString()
{
ostringstream tmp;
tmp << "Value1: " << val1 << " Value2: " << val2 << " Value3: " << val3 << endl;
return tmp.str();
}
};// end Triple class
int main()
{
Triple<int> X(22,33,44);
std::cout << X.ToString() ;
Triple<string> Y("dog","cat","bird");
std::cout << Y.ToString();
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------
File: main4.cpp
#include "cmpslib19.h"
/*
here is a very simple template class example
it will allow you to group 3 values together
into one object
*/
template <class T>
class DynamicArray
{
private:
T * data;
int capacity;
public:
DynamicArray (int size=5){capacity=size;data = new T[size];}
~DynamicArray() {delete [] data;}
T GetVal(int position){return data[position];}
void SetVal(int position, T val)
{
if (position > -1 && position < capacity)
data[position] = val;
else
throw "invalid position";
}
string ToString()
{
ostringstream tmp;
for (int loop =0;loop<capacity;loop++)
tmp << "Position: " << loop << " Value: " << data[loop]<< endl;
return tmp.str();
}
};// end DynamicArray class
int main()
{
DynamicArray<int> da;
da.SetVal(0,55);
da.SetVal(1,66);
da.SetVal(2,77);
da.SetVal(3,88);
da.SetVal(4,99);
cout << da.ToString()<<endl;
DynamicArray<string> da2(3);
da2.SetVal(0,"dog");
da2.SetVal(1,"cat");
da2.SetVal(2,"bird");
cout << da2.ToString()<<endl;
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------