COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
cp9.2vector.cpp
Go to the documentation of this file.
1#include <vector>
2#include <iostream>
3#include <string>
4using namespace std;
5
6void print(vector<string> v) {
7 // vector has two methods to print itself:
8 // either use an iterator or use the [] operator
9 vector<string>::iterator vitr;
10 cout << "Items: ";
11 for (vitr = v.begin(); vitr != v.end(); vitr++) {
12 cout << *vitr;
13 if (vitr != v.end() - 1) {
14 cout << ", ";
15 }
16 }
17 cout << endl;
18}
19
20int main() {
21 vector<string> items;
22 print(items);
23 items.push_back("eggs");
24 items.push_back("milk");
25 items.push_back("sugar");
26 items.push_back("chocolate");
27 items.push_back("flour");
28 print(items);
29 items.pop_back();
30 print(items);
31 items.push_back("coffee");
32 print(items);
33 for (unsigned int i = 0; i < items.size(); i++) {
34 if (items[i] == "sugar") {
35 items[i] = "honey";
36 }
37 }
38 print(items);
39 vector<string>::iterator it;
40 // vector<string> items;
41 for (it = items.begin(); it != items.end(); it++) {
42 if (*it == "milk") {
43 items.erase(it);
44 break;
45 }
46 }
47 print(items);
48 return 0;
49}
void print(vector< string > v)
int main()