forked from jieniyimiao/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.
Merge pull request jieniyimiao#4 from Mooophy/master
SYN
- Loading branch information
Showing
3 changed files
with
50 additions
and
58 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
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
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 |
---|---|---|
@@ -1,72 +1,44 @@ | ||
//! @Alan | ||
//! | ||
//! @author @huangjuncmj @Alan | ||
//! @date 19.11.2014 | ||
//! | ||
//! Exercise 9.26: | ||
//! Using the following definition of ia, copy ia into a vector and into a list. | ||
//! Use the single-iterator form of erase to remove the elements with odd values from your | ||
//! list and the even values from your vector. | ||
// note the subtle difference between list and vector. | ||
//! | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
#include <deque> | ||
#include <list> | ||
|
||
|
||
int main() | ||
{ | ||
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 }; | ||
|
||
//! cal the lenth of the array. | ||
int size = sizeof(ia)/sizeof(int); | ||
|
||
std::vector<int> v; | ||
std::list<int> l; | ||
|
||
// copy in to v and l | ||
for (auto *p = ia; p != ia + size ; ++p) | ||
//! init | ||
std::vector<int> vec; | ||
std::vector<int> lst; | ||
for(auto i : ia) | ||
{ | ||
v.push_back(*p); | ||
l.push_back(*p); | ||
vec.push_back(i); | ||
lst.push_back(i); | ||
} | ||
|
||
// ease even value element from v | ||
for (auto it = v.begin(); it != v.end(); ++it) | ||
{ | ||
if((*it)%2 == 0) | ||
{ | ||
v.erase(it); | ||
} | ||
} | ||
//print content of v | ||
for(auto it = v.begin(); it != v.end(); ++it) | ||
{ | ||
std::cout << *it <<" "; | ||
} | ||
|
||
std::cout << "\n"; | ||
|
||
|
||
//ease odd value element from l | ||
for(auto it = l.begin(); it != l.end(); ++it) | ||
{ | ||
if((*it)%2 == 1) | ||
{ | ||
l.erase(it); | ||
|
||
//! @note without this statement it does not work | ||
--it; | ||
} | ||
} | ||
//print l | ||
for (auto it = l.begin(); it != l.end(); ++it) | ||
{ | ||
std::cout << *it << " "; | ||
} | ||
|
||
|
||
//! remove odd value | ||
for(auto it = lst.begin(); it != lst.end(); ) | ||
if(*it & 0x1) it = lst.erase(it); else ++it; | ||
|
||
//! remove even value | ||
for(auto it = vec.begin(); it != vec.end(); ) | ||
if(!(*it & 0x1)) it = vec.erase(it); else ++it; | ||
|
||
std::cout << "list : "; | ||
for(auto i : lst) std::cout << i << " "; | ||
std::cout << "\nvector : "; | ||
for(auto i : vec) std::cout << i << " "; | ||
std::cout << std::endl; | ||
|
||
return 0; | ||
} |