forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replenish the code of previous commit.
- Loading branch information
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |