#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void printvector(vector<int> vec)
{
cout << "current:\n";
for (auto i : vec)
{
cout << i << " ";
}
cout << endl;
}
vector<int> doublevec(vector<int> vec)
{
vector<int> newvec;
for (auto i : vec)
{
newvec.push_back(i * 2);
}
return newvec;
}
bool findstring(vector<string> haystack, string needle)
{
for (auto i : haystack)
{
if (i == needle) {
return true;
}
}
return false;
}
int findstringlocation(vector<string> haystack, string needle)
{
for (unsigned int i = 0; i < haystack.size(); i++)
{
if (haystack[i] == needle) {
return i;
}
}
return -1;
}
string promptandget()
{
string name;
cout << endl << "Please give an book name: ";
getline(cin, name);
return name;
}
string stringvectostring(vector<string> vec)
{
string out;
for (auto i : vec)
{
out = out + i + ", ";
}
return out;
}
void promptandadd(vector<string> & vec)
{
string name;
cout << endl << "Please give an book name: ";
getline(cin, name);
if (findstring(vec, name)) {
cout << "Sorry but I already have an book by that name";
}
else {
cout << "ok I will add and book by the name of " << name << " to the list of books\n";
vec.push_back(name);
}
}
vector<int> makevec(int x)
{
vector<int> data;
data.push_back(x);
data.push_back(x * 2);
data.push_back(x * 3);
return data;
}
int main()
{
vector<double> firstdata = { 1.1, 2.3 }; // in contrast to what the book says!
vector<double> seconddata;
firstdata.push_back(3.4); // still works
cout << "Here is what is in firstdata:" << endl;
for (int i = 0; i < firstdata.size(); i++) {
cout << firstdata[i] << " ";
}
cout << endl;
cout << "Here is what is in seconddata:" << endl;
for (int i = 0; i < seconddata.size(); i++) {
cout << seconddata[i] << " ";
}
cout << endl;
// Copying Vectors is easier
seconddata = firstdata;
cout << "Here is what is in seconddata:" << endl;
for (int i = 0; i < seconddata.size(); i++) {
cout << seconddata[i] << " ";
}
cout << endl;
// we can also iterate over pointers to the data in the vector
// more on pointers next week, but this is just too messy here anyway!
cout << "\nThis works too:" << endl;
for (vector<double>::iterator i = firstdata.begin(); i != firstdata.end(); i++) {
cout << *i << " ";
}
cout << endl;
cout << "\nThis works and is easier to write:\n";
// We can also say that we want the items in the array one at a time
for (double i : firstdata) {
cout << i << " ";
}
cout << endl;
vector<int> data = { 1, 2 };
cout << endl;
printvector(data);
data = doublevec(data);
printvector(data);
cout << "\nhere is the new one!\n";
vector<int> newvec = makevec(2);
printvector(newvec);
newvec = data;
printvector(newvec);
vector<string> books;
cout << "Add Bible.\n";
books.push_back("Bible");
cout << stringvectostring(books) << endl;
books.push_back(promptandget());
books.push_back(promptandget());
books.push_back(promptandget());
cout << "Added a few more books\n";
cout << stringvectostring(books) << endl;
promptandadd(books);
promptandadd(books);
cout << endl;
cout << "Added a few more books\n";
cout << stringvectostring(books) << endl;
system("pause");
return(0);
}
Back to top