Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cs-142:old-school-vectors [2014/10/21 14:41] (current)
kseppi created
Line 1: Line 1:
 +<code cpp>
 +#include <​iostream>​
 +#include <​vector>​
 +#include <​string>​
 +#include <​algorithm>​
 + 
 +using namespace std;
 + 
 +void printvector(vector<​int>​ vec)
 +{
 + cout << "​current:​\n";​
 + for (unsigned int i = 0; i < vec.size(); i++)
 + {
 + cout << vec[i] << " ";
 + }
 + cout << endl;
 +}
 + 
 +vector<​int>​ doublevec (vector<​int>​ vec)
 +{
 + vector<​int>​ newvec;
 + 
 + for (unsigned int i = 0; i < vec.size(); i++)
 + {
 + newvec.push_back(vec[i] * 2);
 + }
 + return newvec;
 +}
 + 
 +bool findstring (vector<​string>​ haystack, string needle)
 +{
 + for (unsigned int i = 0; i < haystack.size();​ i++)
 + {
 + if (haystack[i] == needle) {
 + return true;
 + }
 + }
 + return false;
 +}
 + 
 +string promptandget ()
 +{
 + string name;
 + cout << endl << "​Please give an book name: ";
 + getline(cin,​name);​
 + return name;
 +}
 + 
 +string stringvectostring(vector<​string>​ vec)
 +{
 + string out;
 + 
 + for (unsigned int i = 0; i < vec.size(); i++)
 + {
 + out = out + vec[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<​int>​ data;
 + 
 + data.push_back(1);​
 + data.push_back(2);​
 + 
 + printvector(data);​
 + 
 + data = doublevec(data);​
 + printvector(data);​
 + 
 + cout << "here 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 << "Added a few more books\n";​
 + 
 + cout << stringvectostring(books) << endl;
 +/*
 + vector<​string>​ flippedbooks = flipbooks(books);​
 + 
 + cout << "​flipped books\n";​
 + 
 + cout << stringvectostring(flippedbooks) << endl;
 + */
 + system("​pause"​);​
 + return(0);
 +}
 +</​code>​
  
cs-142/old-school-vectors.txt ยท Last modified: 2014/10/21 14:41 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0