Skip to content

Commit

Permalink
Refactor : better presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy committed Dec 1, 2014
1 parent ee1aa9d commit 6eb5d33
Showing 1 changed file with 41 additions and 52 deletions.
93 changes: 41 additions & 52 deletions ch10/ex10_6_9_10.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! @Alan
//!
//! @author @Yue Wang @shbling @pezy @zzzkl
//! @date 01.12.2014
//!
//! Exercise 10.6:
//! Using fill_n, write a program to set a sequence of int values to 0.
Expand All @@ -10,7 +12,7 @@
//!
//! Exercise 10.10:
//! Why do you think the algorithms don’t change the size of containers?
// The aim of this desigen is to seperate the algorithms and the operation
// The aim of this design is to seperate the algorithms and the operation
// provided by memeber function.
//!

Expand All @@ -21,65 +23,52 @@
#include <numeric>
#include <list>


//! Exercise 10.6:
void
set0(std::vector<int> &v);

//! Exercise 10.9
void
wy_elimdups(std::vector<std::string> &vs);

int main()
{
//! Exercise 10.6
std::vector<int> v;
v.resize(10);
set0(v);
for(std::vector<int>::iterator iter=v.begin(); iter != v.cend();++iter)
std::cout<<*iter<<std::endl;
//! Exercise 10.9
std::vector<std::string> vs = {"a","v","a","s","a","a","a"};
wy_elimdups(vs);
return 0;
}

//! Exercise 10.6
void set0(std::vector<int> &v)
template<typename Sequence>
inline std::ostream& println(Sequence const& seq)
{
std::fill_n( v.begin(), v.size(), 0 );
for(auto const& elem : seq)
std::cout << elem << " ";
std::cout << std::endl;
return std::cout;
}

//! Exercise 10.9
void wy_elimdups(std::vector<std::string> &vs)
void elimdups(std::vector<std::string> &vs)
{
for (auto element : vs)
std::cout << element
<<" ";
std::cout <<"\n";

println(vs);

//! sort alphabetically
//! sort
std::sort(vs.begin(), vs.end());
for (auto element : vs)
std::cout << element
<<" ";
std::cout <<"\n";

println(vs);

//! put all duplicates at the end of the vector
//! and get the iterator pointing to the one past
//! the last unique element.
auto unique_iterator = std::unique(vs.begin(),vs.end());
for (auto element : vs)
std::cout << element
<<" ";
std::cout <<"\n";
auto new_end = std::unique(vs.begin(),vs.end());
println(vs);

//! erase duplicates
vs.erase(new_end, vs.end());
println(vs);
}

vs.erase(unique_iterator, vs.end());
for (auto element : vs)
std::cout << element
<<" ";
std::cout <<"\n";
int main()
{
//! ex 10.6
std::vector<int> v{1,2,3,4,5,6,7,8,9};
std::fill_n(v.begin(),v.size(),0);
std::cout << "ex10.6:\n";
println(v);

//! ex 10.9
std::vector<std::string> vs{"a","v","a","s","a","a","a"};
std::cout << "ex 10.9:\n";
elimdups(vs);
return 0;
}
//! output:
//ex10.6:
//0 0 0 0 0 0 0 0 0
//ex 10.9:
//a v a s a a a
//a a a a a s v
//a s v a a a a
//a s v

0 comments on commit 6eb5d33

Please sign in to comment.