板凳——————————————————c++(37)

//The c++ standard library 2nd p188
#include
#include

int main(){
std::list coll1;
for(char c = ‘a’; c <= ‘z’; ++c){
coll1.push_back©;
}
//迭代器pos声明于循环之前, 类型是"指向容器内的常量元素"
//std::list::const_iterator pos; //(1)
std::list::iterator pos; //(2)
// for(pos = coll1.begin(); pos != coll1.end(); ++pos){ //++pos效率高 pos++ 内部需要一个临时对象.
// //std::cout << *pos << ’ '; //(1)
// *pos = toupper(*pos); //(2)
// }

// for(auto pos = coll1.begin(); pos != coll1.end(); ++pos){ //(3)
// std::cout << *pos << ’ ';
// }

for(std::list<char>::const_iterator pos = coll1.begin(); pos != coll1.end(); ++pos){ //(4)
     std::cout << *pos << ' ';  
    }
std::cout << std::endl; 

}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17
a b c d e f g h i j k l m n o p q r s t u v w x y z //(1)

wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17 //(2)

wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17 //(3)
a b c d e f g h i j k l m n o p q r s t u v w x y z

wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17 //(4)
a b c d e f g h i j k l m n o p q r s t u v w x y z

#include
#include
#include <unordered_set>

int main()
{
// type of the collection
// typedef std::set IntSet;
// typedef std::unordered_multiset IntSet; //(3)
IntSet coll; // set container for int values
// IntSet coll1;
// insert elements from 1 to 6 in arbitrary order
// - note that there are two calls of insert() with value 1
// coll.insert(3);
// coll.insert(1);
// coll.insert(5);
// coll.insert(4);
// coll.insert(1);
// coll.insert(6);
// coll.insert(2);
//coll.insert ({3, 1, 5, 4, 1, 6, 2}); //(2)
// print all elements
// - iterate over all elements
// IntSet::const_iterator pos;
// for (pos = coll.begin(); pos != coll.end(); ++pos) {
// std::cout << *pos << ’ ';
// }
// std::cout << std::endl;
std::unordered_multiset coll1;
coll1.insert ({3, 1, 5, 7, 11, 13, 17, 19, 23, 27, 1});
for(auto elem : coll1){
std::cout << elem << ’ ';
}
std::cout << std::endl;

coll1.insert(25);

for(auto elem : coll1){
    std::cout << elem << ' ';
}
std::cout << std::endl;

}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17
1 1 2 3 4 5 6

wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17
23 19 17 13 11 7 5 27 1 1 3
25 23 19 17 13 11 7 5 27 1 1 3

//The c++ standard library 2nd p200
#include
#include
#include
#include

int main(){
// std::vector coll {2, 5, 4, 1, 6, 3};
// auto minpos = min_element(coll.cbegin(), coll.cend());
// std::cout << "min: " << *minpos << std::endl;
// auto maxpos = max_element(coll.cbegin(), coll.cend());
// std::cout << "max: " << *maxpos << std::endl;
// //排序, sort() 会改动元素的value, 不能用cbegin()
// sort(coll.begin(), coll.end());
// // 寻找数值为3 的第一个元素, 返回一个迭代器指向目标元素.
// auto pos3 = find( coll.begin(), coll.end(), 3);
// //将区间内的元素反转放置, 必须使用 非常量迭代器
// reverse(pos3, coll.end());
// for(auto elem : coll){
// std::cout << elem << ’ ';
// }
// std::cout << std::endl;

std::list<int> coll1;
for(int i = 20; i <= 40; ++i){
    coll1.push_back(i);
}

auto pos3 = std::find( coll1.begin(), coll1.end(), 3);

std::reverse(pos3, coll1.end());

std::list<int>::iterator pos25, pos30, pos35;
pos25 = std::find( coll1.begin(), coll1.end(), 25);
pos35 = std::find( coll1.begin(), coll1.end(), 35);
// 半开区间, 不包含最后一个元素, 最大值是34
std::cout << "max: " << *max_element(pos25, pos35) << std::endl;
//为了处理最后一个元素, 必须把该元素的下一个位置传给算法
std::cout << "max: " << *max_element(pos25, ++pos35) << std::endl;

++pos35;
pos30 = std::find( pos25, pos35 , 30);

if(pos30 == pos35){
   std::cout << "30 is NOT in the subrange" << std::endl;
}else {
   std::cout << "30 is in the subrange" << std::endl;
}

}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17
min: 1
max: 6
1 2 6 5 4 3

wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17
max: 34
max: 35
30 is in the subrange

//The c++ standard library 2nd p210
#include
#include
#include
#include
#include
#include
#include

int main() //Insert Iterator;
{
std::list coll1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::vector coll2;
// 1,一开始就给出正确大小, 2,要不就显示变更其大小. 3,只适用序列容器.
// resize() 改变coll2 的元素个数.
//coll2.resize(coll1.size());
// RUNTIME ERROR:
// - overwrites nonexisting elements in the destination
copy (coll1.cbegin(), coll1.cend(), // source
std::back_inserter(coll2)); // 容器的最末端
// coll3 初始化时就明确给出足够空间,
// std::deque coll3(coll1.size()); // (1)
// copy (coll1.cbegin(), coll1.cend(),
// coll3.begin());

// std::dequecoll3(coll1.size());
// copy (coll1.cbegin(), coll1.cend(),
// coll3.begin());

      std::deque<int> coll3;
      copy (coll1.begin(), coll1.end(),      // source
      std::front_inserter(coll3));          // // 容器的最前端 
   
   std::set<int> coll4;
   copy (coll1.cbegin(), coll1.cend(),   
          std::inserter(coll4, coll4.begin()));//初始化时接受之第二实参,所指位置的前方插入元素

}

//The c++ standard library 2nd p212
#include
#include
#include
#include
#include

int main() //Stream Iterator
{
std::vector< std::string> coll;

// read all words from the standard input
// - source: all strings until end-of-file (or error)
// - destination: coll (inserting)
copy ( std::istream_iterator< std::string>( std::cin),    // 输入 start of source
       std::istream_iterator< std::string>(),       // end of source
       std::back_inserter(coll));             // destination

// sort elements
 std::sort (coll.begin(), coll.end());                 // 排序

// print all elements without duplicates
// - source: coll
// - destination: standard output (with newline between elements)
 std::unique_copy (coll.cbegin(), coll.cend(),           // 消除毗邻重复值,输出打印source
              std::ostream_iterator< std::string>( std::cout,"\n")); // destination

}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17
World Hello ^d
Hello
World

#include
#include
#include
#include

int main(){ //Reverse Iterator
std::vector coll;
// insert elements from 1 to 9
for (int i=1; i<=9; ++i) {
coll.push_back(i);
}
// print all element in reverse order
copy (coll.crbegin(), coll.crend(), // crbegin() 指向集合的结尾位置 source
std::ostream_iterator(std::cout," ")); // destination
std::cout << std::endl;
}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17
9 8 7 6 5 4 3 2 1

#include
#include

// PRINT_ELEMENTS()
// - prints optional string optstr followed by
// - all elements of the collection coll
// - in one line, separated by spaces
template
inline void PRINT_ELEMENTS (const T& coll,
const std::string& optstr="")
{
std::cout << optstr;
for (const auto& elem : coll) {
std::cout << elem << ’ ';
}
std::cout << std::endl;
}
int main(){
std::string coll;
PRINT_ELEMENTS ( coll, "all elements: ");
return 0;
}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17
all elements:

*/
//The c++ standard library 2nd p218
#include
#include
#include
#include

int main()
{
std::list coll;

// insert elements from 6 to 1 and 1 to 6
for (int i=1; i<=6; ++i) {
    coll.push_front(i);
    coll.push_back(i);
}

// print all elements of the collection
std::cout << "pre:  ";
copy (coll.cbegin(), coll.cend(),         // source
      std::ostream_iterator<int>(std::cout," "));   // destination
std::cout << std::endl;

// // remove all elements with value 3
// remove (coll.begin(), coll.end(), // range // (1)
// 3); // value

// // print all elements of the collection
// std::cout << "post: “;
// copy (coll.cbegin(), coll.cend(), // source
// std::ostream_iterator(std::cout,” ")); // destination
// std::cout << std::endl;

std::list<int>::iterator end = remove (coll.begin(), coll.end(), 3);     // (2)

    // print resulting elements of the collection
    copy (coll.begin(), end,
           std::ostream_iterator<int>(std::cout," "));
    std::cout << std::endl;

    // print number of removed elements
    std::cout << "number of removed elements: "
              << distance(end,coll.end()) << std::endl;

      // remove "removed" elements
    coll.erase (end, coll.end());

    // print all elements of the modified collection
    copy (coll.cbegin(), coll.cend(),
           std::ostream_iterator<int>(std::cout," "));
     std::cout << std::endl;

}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17 // (1)
pre: 6 5 4 3 2 1 1 2 3 4 5 6
post: 6 5 4 2 1 1 2 4 5 6 5 6

wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17 // (2)
pre: 6 5 4 3 2 1 1 2 3 4 5 6
6 5 4 2 1 1 2 4 5 6
number of removed elements: 2
6 5 4 2 1 1 2 4 5 6

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值