Skip to content

Commit

Permalink
replenish the code of previous commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Sep 3, 2014
1 parent eee337d commit c4a277e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
61 changes: 61 additions & 0 deletions ch03/ex3_21_pezy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
///
///Redo the first exercise from § 3.3.3 (p. 105) using iterators.
///

#include <vector>
#include <iterator>
#include <string>
#include <iostream>

using std::vector;
using std::string;
using std::cout;
using std::endl;

void check(const vector<int>& vec)
{
if (vec.empty())
cout << "size: 0; no values." << endl;
else
{
cout << "size: " << vec.size() << "; values:";
for (auto it = vec.begin(); it != vec.end(); ++it)
cout << *it << ",";
cout << "\b." << endl;
}
}

void check(const vector<string>& vec)
{
if (vec.empty())
cout << "size: 0; no values." << endl;
else
{
cout << "size: " << vec.size() << "; values:";
for (auto it = vec.begin(); it != vec.end(); ++it)
if (it->empty()) cout << "(null)" << ",";
else cout << *it << ",";
cout << "\b." << endl;
}
}

int main()
{
vector<int> v1;
vector<int> v2(10);
vector<int> v3(10, 42);
vector<int> v4{10};
vector<int> v5{10, 42};
vector<string> v6{10};
vector<string> v7{10, "hi"};

check(v1);
check(v2);
check(v3);
check(v4);
check(v5);
check(v6);
check(v7);

return 0;
}
30 changes: 30 additions & 0 deletions ch03/ex3_22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
///
///Revise the loop that printed the first paragraph in text
///to instead change the elements in text that correspond
///to the first paragraph to all uppercase.
///After you’ve updated text, print its contents.
///

#include <iostream>
#include <vector>
#include <string>
#include <iterator>

using std::vector; using std::string; using std::cout; using std::cin; using std::endl;

int main()
{
vector<string> vec;
string str;
while (getline(cin, str))
if (!str.empty()) vec.push_back(str);

auto first = vec.begin();
for (auto& c : *first)
c = toupper(c);

for (auto it = vec.begin(); it != vec.end(); ++it)
cout << *it << endl;

return 0;
}
23 changes: 23 additions & 0 deletions ch03/ex3_23.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
///
///Write a program to create a vector with ten int elements.
///Using an iterator, assign each element a value that is twice its current value.
/// Test your program by printing the vector.
///

#include <vector>
#include <iostream>
#include <iterator>

using std::vector; using std::iterator; using std::cout; using std::endl;

int main()
{
vector<int> ivec(10, 1);
for (auto it = ivec.begin(); it != ivec.end(); ++it)
*it *= 2;
for (auto value : ivec)
cout << value << " ";
cout << endl;

return 0;
}

0 comments on commit c4a277e

Please sign in to comment.