1.
#include<iostream>
#include<vector>
using namespace std;
void print(vector<int> v)
{
cout<<v.size()<<endl;
for(vector<int>::iterator iter =v.begin();iter!=v.end();iter++)
cout<<*iter<<" ";
cout<<endl<<endl;
}
int sum(vector<int> v)
{
int s=0;
vector<int>::iterator iter =v.begin();
while(iter!=v.end())
s+=*iter++;
return s;
}
int main(int argc, char* argv[])
{
vector<int> vec;
vec.push_back(34);
vec.push_back(23);
print(vec);
vector<int>::iterator p =vec.begin();
*p=68;
*(p+1)=69;
print(vec);
vec.pop_back();
print(vec);
vec.push_back(101);
vec.push_back(102);
vec[0]=1000;
vec[1]=1001;
vec[2]=1002;
int i=0;
while(i<vec.size())
cout<<vec[i++]<<" ";
cout<<endl;
vector<int> v1(100);
cout<<v1.size()<<endl; //100
cout<<sum(v1)<<endl; //0
v1.push_back(23);
cout<<v1.size()<<endl; //100
cout<<sum(v1)<<endl; //23
v1.reserve(1000);
v1[900]=900;
cout<<v1[900]<<endl; //900
cout<<v1.front()<<endl; //0
cout<<v1.back()<<endl; //23
v1.pop_back();
cout<<v1.back()<<endl; //0
}
2.
#include<iostream>
#include<vector>
using namespace std;
class CDemo
{
public :
CDemo():str(NULL){}
CDemo(const CDemo & cd)
{
str = new char[strlen(cd.str)+1];
strcpy(str,cd.str);
}
~CDemo()
{
if(str)
delete [] str;
}
char *str;
};
int main()
{
CDemo d1;
d1.str=new char[32];
strcpy(d1.str,"hello");
vector<CDemo> *a1=new vector<CDemo>();
a1->push_back(d1);
delete a1; //如果不定义拷贝构造函数将出现push_back的时候的浅拷贝情况,析构a1的时候要
//delete 类中的str,导致str被delete两次
}
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void print(vector<int> v)
{
cout<<v.size()<<endl;
for(vector<int>::iterator iter =v.begin();iter!=v.end();iter++)
cout<<*iter<<" ";
cout<<endl<<endl;
}
int main()
{
vector<int> vec;
vec.push_back(3);
vec.push_back(6);
vec.push_back(6);
vec.push_back(3);
vector<int>::iterator iter =vec.begin();
vector<int>::iterator iter2;
print(vec);
for(;iter!=vec.end();iter++)
{
if(6==*iter)
{
iter2=iter;
vec.erase(iter2);
iter--; //回到删除之前的位置上,因为此时iter指向下一个位置,再自增错过第二个'6'
print(vec); // 3 2
}
}
vec.erase(remove(vec.begin(),vec.end(),3),vec.end()); //0
print(vec);
}
3.
const int *find1(const int* array,int n,int x)
{
const int * p=array;
for(int i=0;i<n;i++)
{
if(*p==x)
return p;
++p;
}
return 0;
}
改为泛型函数
template<typename T>
const T *find1(const T* array, T n, T x)
{
const T * p=array;
for(int i=0;i<n;i++)
{
if(*p==x)
return p;
++p;
}
return 0;
}
4.
#include<iostream>
using namespace std;
template<typename T>
class operate
{
public :
static T add(T x,T y)
{
return x+y;
}
static T mul(T x,T y)
{
return x*y;
}
static T judge(T x,T y)
{
if(x>=0)
return x;
else
return x/y;
}
};
int main()
{
cout<<operate<int>::add(1,2)<<endl;
cout<<operate<int>::mul(1,2)<<endl;
cout<<operate<int>::judge(1,2)<<endl;
}
</pre><pre name="code" class="cpp">#include<iostream>
using namespace std;
int sub(int x,int y)
{
return x+y;
}
void test(int (*p)(int ,int ),int a ,int b) //函数指针
{
cout<<(*p)(a,b);
}
int main()
{
test(sub,1,2);
}
5.
#include<iostream>
using namespace std;
template<typename T>
struct tcontainer
{
virtual void push(const T&)=0;
virtual void pop()=0;
virtual const T& begin()=0;
virtual const T& end()=0;
virtual size_t size()=0;
};
template<typename t>
struct tvector:public tcontainer<t>
{
static const size_t step=100;
tvector()
{
size_=0;
cap=step;
buf=0;
re_capacity(cap);
}
~tvector()
{
if(buf)
delete []buf;
}
void re_capacity(size_t s)
{
if(!buf)
buf = new t[s];
else
buf=(t*)realloc(buf,sizeof(t)*s);
}
virtual void push(const t& v)
{
if(size_>=cap)
re_capacity(cap+=step);
buf[size_++]=v;
}
virtual void pop()
{
if(size_)
size_--;
}
virtual const t& begin()
{
if(size_)
return buf[0];
}
virtual const t& end()
{
if(size_)
return buf[size_-1];
}
virtual size_t size()
{
return size_;
}
const t&operator[](size_t i)
{
if(i>=0 && i<size_)
return buf[i];
}
private:
size_t size_;
size_t cap;
t* buf;
};
int main()
{
tvector<int> v;
for(int i=0;i<10;i++)
v.push(i);
v.pop();
for(int i=0;i<v.size();i++)
cout<<v[i];
cout<<endl;
cout<<v.begin()<<endl;
cout<<v.end()<<endl;
}
6.
#include<iostream>
#include<vector>
using namespace std;
bool used[100];
vector<long long > v;
void dfs(int k ,long long a)
{
if(k && a%k!=0)
return ;
if(k==9)
{
v.push_back(a);
return ;
}
for(int i=1;i<=9;i++)
{
if(!used[i])
{
used[i]=1;
dfs(k+1,a*10+i);
used[i]=0;
}
}
}
int main()
{
dfs(0,0);
for(int i=0;i<v.size();i++)
cout<<v[i];
}