#include
#include
using namespace std;
int main(){
string s1("hello");
cout<string s2(8, 'x');
cout<string match = "matrch";
cout<string s;
s = 'n';
cout<return 0;
}
hello
xxxxxxxx
matrch
n
string s1(“hello world”), s2;
s2 = s1.substr(4,5); //从下标为4的位置,向后数5个字符进行赋值
#include
#include
#include
using namespace std;
int main(){
string s1("hello world");
string ss1, ss2;
istringstream inputstring(s1);
inputstring>>ss1>>ss2;
cout<" "<return 0;
}
#include
#include
#include
using namespace std;
int main(){
string s1("hello world");
string ss1, ss2;
istringstream inputstring(s1);
inputstring>>ss1>>ss2;
ostringstream out;
out<<"the "<<22<<" ok"<cout<cout<<"there";
return 0;
}
the 22 ok
there
#include
#include
using namespace std;
int main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
vector<int>::const_iterator i;
for (i = v.begin(); i != v.end(); ++i) {
cout<<*i<<",";
}
cout<vector<int>::reverse_iterator r;//反向迭代器,这里r++会指向容器中的前一个元素
for (r = v.rbegin(); r!=v.rend(); ++r) {
cout<<*r<<",";
}
vector<int>::iterator j; //非常量迭代器
for (j = v.begin(); j != v.end() ; ++j) {
*j=100;
}
cout<for(auto x:v){
cout<",";
}
}
1,2,3,4,
4,3,2,1,
100,100,100,100,
list<int> vv;
vv.push_back(1);
vv.push_back(3);
vv.push_back(5);
list<int> ::const_iterator ii;
//这里不能用ii
for (ii=vv.begin();ii!=vv.end();++ii){
cout<<*ii<<",";
}
#include
#include
using namespace std;
class A{
int v;
public:
A(int n):v(n){}
bool operator < (const A &a2)const{
cout<"<"<"?"<return false;
}
bool operator == (const A &a2) const{
cout<"=="<"?"<return v==a2.v;
}
};
int main()
{
A a[]={A(1), A(2), A(3), A(4), A(5)};
cout<4, A(9));//折半查找中,只是判断了是否小于。。返回1是找到了
return 0;
}
3<9?
2<9?
1<9?
9<1?
1
#include
#include
using namespace std;
template <class T>
void PrintVector(T s, T e){
for(;s!=e;++s)
cout<<*s<<" ";
cout<int main(){
int a[5]={1,2,3,4,5};
vector<int> v(a, a+5);
cout<<"1)"<cout<<"2)";
PrintVector(v.begin(), v.end());
v.insert(v.begin()+2, 13);//插入。。第一个参数是位置,第二个位置是元素
cout<<"3)";
PrintVector(v.begin(), v.end());
v.erase(v.begin()+2);//删除
cout<<"4)";
PrintVector(v.begin(), v.end());
vector<int> v2(4, 100);//4个元素,每个元素都是100
v2.insert(v2.begin(),v.begin()+1, v.begin()+3);//在v2里面插入某个区间
cout<<"5) v2:";
PrintVector(v2.begin(), v2.end());
v.erase(v.begin()+1, v.begin()+3);
cout<<"6) ";
PrintVector(v.begin(), v.end());
return 0;
}
1)5
2)1 2 3 4 5
3)1 2 13 3 4 5
4)1 2 3 4 5
5) v2:2 3 100 100 100 100
6) 1 4 5
#include
#include
using namespace std;
int main()
{
vector< vector<int> > v(3);
for (int i = 0; i < v.size(); ++i) {
for (int j = 0; j < 4; ++j) {
v[i].push_back(j);
}
}
for (int i = 0; i < v.size(); ++i) {
for (int j = 0; j < 4; ++j) {
cout<" ";
}
cout<return 0;
}
0 1 2 3
0 1 2 3
0 1 2 3
#include
#include
#include
using namespace std;
class A{
private:
int n;
public:
A(int _n):n(_n){}
friend bool operator<(const A &a1, const A &a2);
friend bool operator==(const A &a1, const A &a2);
friend ostream &operator<<(ostream &o, const A &a);
};
bool operator<(const A &a1, const A &a2) {
return a1.nbool operator==(const A &a1, const A &a2) {
return a1.n==a2.n;
}
ostream& operator<<(ostream &o, const A &a) {
o<return o;
}
template <class T>
void PrintList(const list & lst){
//不推荐的写法,用两个迭代器更好
typename list ::const_iterator i; //typename用来说明list::const_iterator是个类型
i = lst.begin();
for (i = lst.begin(); i!=lst.end(); ++i) {
cout<<*i<<",";
}
}
int main()
{
list lst1, lst2;
lst1.push_back(1);lst1.push_back(3);
lst1.push_back(2);lst1.push_back(4);
lst1.push_back(2);
lst2.push_back(10);
lst2.push_front(20);
lst2.push_back(30);
lst2.push_front(30);
lst2.push_back(30);
lst2.push_front(40);
lst2.push_back(40);
cout<<"1)";
PrintList(lst1);cout<cout<<"2)";
PrintList(lst2);cout<cout<<"3)";
PrintList(lst2);
cout<//删掉头部元素
cout<<"4)";
PrintList(lst2);
cout<2); //删除和A(2)相等的元素
cout<<"5)";
PrintList(lst1);
cout<cout<<"6)";
PrintList(lst2);
cout<//合并lst2到lst1并清空lst2
cout<<"7)";
PrintList(lst1);
cout<cout<<"8)";
PrintList(lst2);
cout<cout<<"9)";
PrintList(lst1);
cout<100);
lst2.push_back(200);
lst2.push_back(300);
lst2.push_back(400);
list::iterator p1, p2, p3;
p1 = find(lst1.begin(), lst1.end(), 3);
p2 = find(lst2.begin(), lst2.end(), 200);
p3 = find(lst2.begin(), lst2.end(), 400);
lst1.splice(p1, lst2, p2, p3);//将lst2[p2,p3)插入到p1之前, 并从lst2中删除[p2, p3)
cout<<"10)";
PrintList(lst1);
cout<cout<<"11)";
PrintList(lst2);
cout<return 0;
}
1)1,3,2,4,2,
2)40,30,20,10,30,30,40,
3)10,20,30,30,30,40,40,
4)20,30,30,30,40,40,
5)1,3,4,
6)20,30,40,
7)1,3,4,20,30,40,
8)
9)40,30,20,4,3,1,
10)40,30,20,4,200,300,3,1,
11)100,400,
#include
#include
#include
#include
#include
using namespace std;
int SumSquares(int total, int value)
{return total+value*value;}
template <class T>
void PrintInterval(T first, T last)
{
for(;first!=last;++first)
{
cout<<*first<<" ";
}
cout<template <class T>
class SumPowers
{
private:
int power;
public:
SumPowers(int p):power(p){}
const T operator()(const T&total, const T&value){
T v=value;
for(int i=0;i1;i++) v=v*value;
return total+v;
}
};
int main()
{
const int SIZE = 10;
int a1[]={1,2,3,4,5,6,7,8,9,10};
vector<int> v(a1, a1+SIZE);
cout<<"1)";
PrintInterval(v.begin(), v.end());
int result = accumulate(v.begin(), v.end(), 0, SumSquares);//SumSquares是个函数
/*
这里实例化了:
int accumulate(vector::iterator first, vector::iterator last, int init, int(*op)(int, int))
{
for(;first!=last;++first)
init=op(init, *first);
return init;
}
*/
cout<<"2)平方和"<//SumPowers是个类模板,SumPowers模板类,SumPowers(3)是个对象,这个对象重载了“()”
result = accumulate(v.begin(), v.end(), 0, SumPowers<int>(3));
/*
int accumulate(vector::iterator first, vector::iterator last, int init, SumPowers op)
{
for(;first!=last;++first)
init=op(init, *first);
return init;
}
*/
cout<<"3)立方和"<0, SumPowers<int>(4));
cout<<"4)4次方和"<return 0;
}
1)1 2 3 4 5 6 7 8 9 10
2)平方和385
3)立方和3025
4)4次方和25333
//在c++源码中如下:
template<typename _InputIterator, typename _Tp>
inline _Tp
accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_requires_valid_range(__first, __last);
for (; __first != __last; ++__first)
__init = __init + *__first;
return __init;
}
template<typename _InputIterator, typename _Tp, typename _BinaryOperation>
inline _Tp
accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
_BinaryOperation __binary_op)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_requires_valid_range(__first, __last);
for (; __first != __last; ++__first)
__init = __binary_op(__init, *__first);
return __init;
}
//在c++中源码如下:
template<typename _Tp>
struct greater : public binary_function<_Tp, _Tp, bool>
{
_GLIBCXX14_CONSTEXPR
bool
operator()(const _Tp& __x, const _Tp& __y) const
{ return __x > __y; }
};
#include
#include
using namespace std;
class MyLess{
public:
bool operator()(const int &c1, const int &c2)
{
//比较各位数
return (c1%10)<(c2%10);
}
};
template <class T>
void Print(T first, T last){
for(;first!=last;++first) cout<<*first<<",";
}
int main()
{
const int SIZE=5;
int a[SIZE]={5,21,14,2,3};
list<int> lst(a, a+SIZE);
lst.sort(MyLess());
Print(lst.begin(), lst.end());
cout<int>());//greater()是个对象
Print(lst.begin(), lst.end());
cout<return 0;
}
21,2,3,14,5,
21,14,5,3,2,
#include
#include
using namespace std;
class MyLess{
public:
bool operator()(int a1, int a2){
if((a1%10)<(a2%10)) return true;
else return false;
}
};
bool MyCompare(int a1, int a2){
if((a1%10)<(a2%10)) return false;
else return true;
}
template <class T, class pred>
T MyMax(T first, T last, pred op){
T temp = first;
for(;first!=last;++first){
if(op(*temp, *first)) ////如果*temp小于*first
temp = first;
}
return temp;
};
int main()
{
int a[]={35, 7,13, 19, 12};
cout<<*MyMax(a, a+5, MyLess())<cout<<*MyMax(a, a+5, MyCompare)<return 0;
}
19
12