Skip to content

Commit

Permalink
Merge pull request jieniyimiao#4 from Mooophy/master
Browse files Browse the repository at this point in the history
SYN
  • Loading branch information
Queequeg92 committed Nov 20, 2014
2 parents e3ee746 + 643ae39 commit 1132ba6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 58 deletions.
22 changes: 17 additions & 5 deletions ch09/ex9_13.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! @Alan
//! @author @shbling @Alan
//!
//! Exercise 9.13:
//! How would you initialize a vector<double> from a list<int>?
Expand All @@ -8,15 +8,27 @@
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>

using std::list;using std::vector;using std::cout;using std::endl;

int main()
{
std::list<int> ls(10,1);
std::vector<int> vi(10,2);
list<int> ilst(5, 4);
vector<int> ivc(5, 5);

//! from list<int> to vector<double>
vector<double> dvc (ilst.begin(),ilst.end());
for (auto i : ilst) cout << i;
cout << endl;
for (auto t : dvc) cout << t;
cout << endl;

//! from vector<int> to vector<double>
vector<double> dvc2(ivc.begin(), ivc.end());
for (auto i : ivc) cout << i;
cout << endl;
for (auto t : dvc2) cout << t;

std::vector<double> v(vi.begin(), vi.end());
return 0;
}
16 changes: 12 additions & 4 deletions ch09/ex9_15_16_17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ bool is_equal(const std::vector<int> &v1, const std::vector<int> &v2);
bool is_equal2(const std::vector<int> &v, const std::list<int> &l);
int main()
{
std::list<char*> l(10);
std::vector<std::string> v;
v.assign(l.cbegin(), l.cend());
std::vector<int> iv{ 10, 1 };
std::vector<int> iv2{ 10, 1 };
std::list<int> il{ 10, 2 };

if (is_equal(iv, iv2))//EX9.15
std::cout << "iv and iv2 are equal;" << std::endl;
else std::cout << "iv and iv2 are different;" << std::endl;

return 0;
if (is_equal2(iv, il))//EX9.16
std::cout << "iv and il are equal." << std::endl;
else std::cout << "iv and il are different." << std::endl;

return 0;
}


Expand Down
70 changes: 21 additions & 49 deletions ch09/ex9_26.cpp
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;

//! print
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;
}

0 comments on commit 1132ba6

Please sign in to comment.