C++学习笔记,粗糙原代码
第一次写博客,后面慢慢完善吧,现在先记录一下学习的代码。
目前在按照Essential C++这本书学习,觉得讲的还是不错的,适合小白。我目前学到了第三章第六小节,希望寒假能把这本书学完吧(我看后面东西挺多的)
这是第三章前五节
#include<iostream>
#include<vector>
#include<string>
#include<list>
#include<iterator>
#include<deque>
#include<algorithm>
#include<functional>
using namespace std;
template<typename IteratorType, typename elemType>
IteratorType find(IteratorType &first, IteratorType &last,
const elemType& value);//find函数的声明
int main()
{
//这一段只是简单的测试泛型指针iterator
//vector<string>svec;
//vector<string>::iterator iter = svec.begin();
//string sa[4] = { "one","two","three","four" };
//const vector<string>cs_vec(sa,sa+4);
//string sa[4] = { "one","two","three","four" };
//const vector<string>cs_vec(sa,sa+4);
//测试find函数
const int asize = 8;
int ia[asize] = { 1,1,2,3,5,8,13,21 };
vector<int>ivec(ia, ia + asize);
list<int>ilist(ia, ia + asize);
int* pia = find(ia, ia + asize, ia[3]);
//array
if (pia != ia + asize)
{
cout << find(ia, ia + asize, ia[3]);
}
//vector
vector<int>::iterator it;
it = find(ivec.begin(), ivec.end(), ia[3]);
if (it != ivec.end())
{
cout << "find it";
cout << *it;
//cout << find(ivec.begin(), ivec.end(), ia[3];)
//为什么有函数的时候会报错呢?
//不能直接cout<<find, 需要用泛型指针来代替
}
//list
list<int>::iterator iter;
iter = find(ilist.begin(), ilist.end(), ia[5]);
if (iter != ilist.end())
{
cout << "find list";
}
list<string>slist(16, "unassigned");//产生特定大小容器,并指定初值
cout << slist.front();
cout << slist.max_size() << endl;
list<string>slist2(slist);
//使用deque添加删除元素
//push_back()在最末端安插一个元素
//pop_back()删除最后一个元素
//list和deque提供push_front()和pop_front(),在最前端安插和删除元素
deque<int>a_line;
int ival;
while (cin >> ival)
{
a_line.push_back(ival);
int curr_value = a_line.front();
cout << curr_value;
a_line.pop_front();
}
return 0;
}
//定义了find函数,返回地址
template<typename IteratorType,typename elemType>
IteratorType find(IteratorType &first,IteratorType &last,
const elemType &value)
{
for (; first != last; ++first)
{
if (value == *first)
{
return first;
}
}
return last;
}
下面是第六小节的代码,有用到function object,我觉得多熟悉记忆就好。
//用函数指针的filter声明
//vector<int>filter(const vector<int>& vec,
// int filter_value,
// bool (*pred)(int, int));
vector<int>filter(const vector<int>& vec,
int val, less<int>& lt);//用function object的声明
template<typename InputIter, typename OutputIter,//模板函数
typename ElemType, typename Comp>
OutputIter filter2(InputIter first, InputIter last,
OutputIter at, const ElemType& val,
Comp pred);//经过模板函数修改,消除了和元素型别关联的过滤函数
bool less_than(int v1, int v2);//比较函数,用函数指针,下同
bool greater_than(int v1, int v2);
int count_occurs(const vector<int>& vec, int val);//显示某个数出现次数的函数
int main()
{
int ia[10] = { 1,2,3,10,7 ,13,8,9,16,22, };
vector<int>ivec(ia, ia + 10);
int value = 3;
less<int>lt;//为什么这里定义成指针需要初始值呢?
vector<int>lt_10 = filter(ivec, value, lt);
for (int i = 0; i < lt_10.size(); i++)
{
cout << lt_10[i];
cout << '\n';
}
//cout << '\n';
//测试出现某个数字次数的函数
//cout << count_occurs(big_vec, 10);
//测试sort 排序函数
sort(ivec.begin(),ivec.end());
for (int i = 0; i < ivec.size(); i++)
{
cout << ivec[i]<<' ';
}
cout << '\n';
sort(ivec.begin(), ivec.end(), greater<int>{});
for (int i = 0; i < ivec.size(); i++)
{
cout << ivec[i]<<' ';
}
//利用transform,对Fib数列进行相加,相乘等
int ic[5] = { 1,1,2,3,5 };
int ib[5] = { 1,1,1,1,2 };
vector<int>fib(ic, ic + 5);
vector<int>pell(ib, ib + 5);
vector<int>fib_plus_pell(5);
transform(fib.begin(), fib.end(), pell.begin()
, fib_plus_pell.begin(), multiplies<int>());
for (int i = 0; i < fib_plus_pell.size(); i++)
{
cout << fib_plus_pell[i] << '\t';
}
//测试模板函数 filter2
const int elem_size = 8;
int ix[elem_size] = { 12,8,46,0,6,21,3,7 };
vector<int>ivec(ix, ix + elem_size);
//下面这个容器用来存储过滤结果
int ix2[elem_size];
vector<int>ivec2(elem_size);
cout << "filtering integer array for values less than 8\n";
filter2(ix, ix + elem_size, ix2, elem_size, less<int>());
cout << "filtering interger vector for values greater than8\n";
filter2(ivec.begin(), ivec.end(), ivec2.begin(), elem_size, greater<int>());
}
//使用函数指针引入小于函数
//vector<int>filter(const vector<int>& vec,
// int filter_value,bool (*pred)(int, int))
//使用 binder adapter 绑定配接器 Function object adapter
//会提高效率
vector<int>filter(const vector<int>& vec,
int val,less<int><)
{
vector<int> nvec;
vector<int>::const_iterator iter = vec.begin();
//bind2nd(less<int>,val);
//会把val绑定于less<int>的第二个参数身上。
//于是,less<int>会将每个元素拿来和val比较
while ((iter = find_if(iter, vec.end(),
bind2nd(lt, val))) != vec.end())
{
//每当iter!=vec.end(),
//iter 便是指向某个小于val的元素
nvec.push_back(*iter);
iter++;
}
return nvec;
}
//将函数filter()改为function template
//消除filter和vector元素型别的关联
template<typename InputIter,typename OutputIter,
typename ElemType,typename Comp>
OutputIter filter2(InputIter first, InputIter last,
OutputIter at, const ElemType& val,
Comp pred)
{
while ((first =
find_if(first, last, bind2nd(pred, val))) != last)
{
cout << "found value:" << *first << endl;
//执行assign操作,然后令俩个iterators 前进
*at++ = *first++;
}
return at;
}
//小于函数
bool less_than(int v1, int v2)
{
return v1 < v2 ? true : false;
}
//大于函数
bool greater_than(int v1, int v2)
{
return v1 > v2 ? true : false;
}
//返回某个数字出现次数的函数
int count_occurs(const vector<int>& vec, int val)
{
vector<int>::const_iterator iter = vec.begin();
int occurs_count = 0;
while ((iter = find(iter, vec.end(), val)) != vec.end())
{
++occurs_count;
++iter;
}
return occurs_count;
}
以后继续努力。
来源:CSDN
作者:Alva丶
链接:https://blog.csdn.net/qq_45712717/article/details/104128896