C++:map计数器

问题

单词计数程序.

C++.
image-2136

源码

1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*************************************************************************
    > File Name: test.11.3.cpp
    > Author: puruidong
    > Mail: 1@w1520.com
    > Created Time: 2014年06月27日
 ************************************************************************/

#include<iostream>
#include<map>
using namespace std;

/*************************************
 
    编写你自己的单词计数程序.


 **********************************************/


 int main()
 {
     map<string,size_t> word_count;//统计.
     string word;
     cout << "输入单词:" << endl;
     while(cin >> word)
     {
         ++word_count[word];//增加次数.
     }
     cout << "********************\n输出统计结果\n*****************" << endl;
     for(const auto &w : word_count)
     {
         cout << w.first << "\t>>>\t" << w.second << endl;
     }
     return 0;
 }

C++:成员函数算法运算

问题

使用链表类型list或forward_list成员函数算法时,其会改变底层的容器.这样做,会让指向被删除元素的迭代器、引用和指针都会失效!而使用失效的迭代器、指针或引用时严重的运行时错误!!

使用list成员函数算法编写一个单词去重的程序.

C++.
image-2134

源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*************************************************************************
    > File Name: test.10.42.cpp
    > Author: puruidong
    > Mail: 1@w1520.com
    > Created Time: 2014年06月24日
 ************************************************************************/

#include<iostream>
#include<list>
#include<algorithm>
#include<functional>
using namespace std;
using namespace  std::placeholders;

/****************************************

    使用list成员函数算法编写一个单词去重的程序.


****************************************************/

bool issh(const string &s1,const size_t sz)
{
    return s1.size()>sz;
}

//使用成员函数算法.
void elis(list<string> &lst,size_t sz)
{
    lst.sort();
    lst.unique();
    auto rf = find_if(lst.begin(),lst.end(),bind(issh, _1 , sz ));
    //auto ws = lst.end()-rf;
    cout << "******************************\n下面是大于" << sz << "的元素\n**********************************" << endl;
    for_each(rf,lst.end(),[](const string &a){ cout << a << " ";});
    cout << endl;
}

int main()
{
    cout << "使用list替代vector实现去重的程序:" << endl;
    string pa;
    list<string> lst;
    while(cin >> pa)
    {
        lst.push_back(pa);
    }
    elis(lst,5);
    return 0;
}

C++:逆序拷贝元素

问题

给定一个包含10个元素的vector,
将位置3到7之间的元素按逆序拷贝到一个list中.

C++.
image-2132

源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*************************************************************************
    > File Name: test.10.37.cpp
    > Author: puruidong
    > Mail: 1@w1520.com
    > Created Time: 2014年06月23日
 ************************************************************************/

#include<iostream>
#include<algorithm>
#include<vector>
#include<iterator>
#include<list>
using namespace std;

/*****************************
 

 给定一个包含10个元素的vector,将位置3到7之间的元素按逆序拷贝到一个list中.

 **********************************/



 int main()
 {
     vector<int> vec={0,1,2,3,4,5,6,7,8,9};
     list<int> lst;
     vector<int>::reverse_iterator it=vec.rbegin();  
         lst.insert(lst.begin(),(it+3),(it+7));
   
     for(auto &s:lst)
     {
         cout << s << endl;
     }
     return 0;
 }

C++:流迭代器读写数据

问题

编写程序,接受三个参数:一个输入文件和两个输出文件的文件名.
输入文件保存的应该是整数.使用istream_iterator读取输入文件.使用
ostream_iterator将奇数写入第一个输出文件,每个值之后都跟一个空格.将偶数
写入第二个输出文件,每个值都独占一行.

C++.
image-2130

源代码

必须提供三个文件:一个里面有数据,另外两个空文件.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*************************************************************************
    > File Name: test.10.33.cpp
    > Author: puruidong
    > Mail: 1@w1520.com
    > Created Time: 2014年06月22日
 ************************************************************************/

#include<iostream>
#include<fstream>
#include<vector>
#include<iterator>
#include<algorithm>
using namespace std;

/*********************************
 
 编写程序,接受三个参数:一个输入文件和两个输出文件的文件名.
 输入文件保存的应该是整数.使用istream_iterator读取输入文件.使用
 ostream_iterator将奇数写入第一个输出文件,每个值之后都跟一个空格.将偶数
 写入第二个输出文件,每个值都独占一行.

 *****************************************/


void outs(string file,bool wr)
{
    fstream f(file);
    istream_iterator<string> ins(f);
    istream_iterator<string> eof;
    vector<string> vecs(ins,eof);
    if(wr)
    {
    ostream_iterator<string> outins(cout," ");
    copy(vecs.begin(),vecs.end(),outins);
    }else
    {
    ostream_iterator<string> outins(cout,"\n");
    copy(vecs.begin(),vecs.end(),outins);    
    }
}




void rwfile(string readfile,string writefile1,string writefile2)
 {
     fstream f(readfile);
     fstream fw1(writefile1);
     fstream fw2(writefile2);
     istream_iterator<int> ins(f);
     istream_iterator<int> eof;
     vector<int> vec(ins,eof);
    //cout << vec.size() << endl;
    // unique_copy(ins,eof,vec);
     ostream_iterator<int> out1(fw1," ");
     ostream_iterator<int> out2(fw2,"\n");
     fw1 << "**********************\n奇数\n*******************" << endl;
     fw2 << "*********************\n偶数\n********************" << endl;
     for(vector<int>::iterator it=vec.begin();it!=vec.end();++it)
     {
         if(*it%2==0)
         {
             out2=*it;
         }else
         {
            out1 = *it;
         }
     }
     fw1 << "\n*****************奇数******" << endl;
    fw2 << "\n*******************偶数********" << endl;
     bool wr = true;
     outs(writefile1,wr);
     cout << "\n\n*************此输出为程序再次读取文件[已写入完成]的输出,对格式进行了处理,但是未改变源文件格式,具体可看源文件.******************\n\n" << endl;
     outs(writefile2,wr=false);
 }


int main()
{
 //rwfile
    rwfile("test.10.33.readfile.txt","test.10.33.rout1.txt","test.10.33.rout2.txt");
    return 0;
}

C++:判断拷贝元素的顺序

问题

一个vector中保存1到9,将其拷贝到三个其他容器中。使用inserter,
back_inserter和front_inserter将元素添加到三个容器中,
对每种inserter估计输出序列是怎样的,运行程序验证你的估计是否正确.

C++.
image-2120

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*************************************************************************
    > File Name: test.10.28.cpp
    > Author: puruidong
    > Mail: 1@w1520.com
    > Created Time: 2014年06月20日 星期五 23时28分11秒
 ************************************************************************/

#include<iostream>
#include<vector>
#include<list>
#include<algorithm>
#include<deque>
#include<forward_list>
using namespace std;

/*************************************************
 
    一个vector中保存1到9,将其拷贝到三个其他容器中。使用inserter,
    back_inserter和front_inserter将元素添加到三个容器中,
    对每种inserter估计输出序列是怎样的,运行程序验证你的估计是否正确.


 **********************************************************/

int main()
{
    vector<int> vec ={1,2,3,4,5,6,7,8,9};
   forward_list<int> fwlt ;
    list<int> lst;
    deque<int> dqe;
    cout << "使用front_inserter添加数据到forward_list,将是倒序的." << endl;
    copy(vec.begin(),vec.end(),front_inserter(fwlt));
    cout << "forward_list的内容:" << endl;
    for(const auto &fw:fwlt)
    {
        cout << fw << endl;
    }
    cout << "使用inserter添加数据到list,顺序不变." << endl;
    copy(vec.begin(),vec.end(),inserter(lst,lst.begin()));
    cout << "list的内容:" << endl;
    for(const auto &ls : lst)
    {
        cout << ls << endl;
    }
    cout << "使用back_inserter添加数据到deque不会改变顺序" << endl;
    copy(vec.begin(),vec.end(),back_inserter(dqe));
    cout << "deque的内容:" << endl;
    for(const auto &dq : dqe)
    {
        cout << dq << endl;
    }
    return 0;
}