001
#include<iostream>
using namespace std;
void swap(int &v1,int &v2)
{
int temp;
temp = v2;
v2 = v1;
v1 = temp;
}
void addOne(int x)
{
x = x +1;
}
void addTwo(int &x)
{
x = x +2;
}
int main()
{
int i = 10;
int j = 20;
cout<<"before swap():"<<i<<" "<<j<<endl;
swap(i,j);
cout<<"after swap():"<<i<<" "<<j<<endl;
int x = 1,y = 2;
addOne(x);
addTwo(y);
cout<<x<<" "<<y<<endl;
return 0;
}
002
#include<iostream>
using namespace std;
void doOp(int x,int y,int &result1,int &result2,int &result3,int &result4)
{
result1 = x + y;
result2 = x - y;
result3 = x * y;
result4 = x / y;
}
bool isShorter(const string &s1,const string &s2)
{
return s1.size()<s2.size();
}
string::size_type find_char(const string &s,char c)
{
string::size_type i = 0;
while(i != s.size() && s[i] != c)
++i;
return i;
}
int main()
{
int a = 10,b = 2;
int result1,result2,result3,result4;
doOp(a,b,result1,result2,result3,result4);
cout<<result1<<" "<<result2<<" "<<result3<<" "<<result4<<endl;
string s1("one");
string s2("hello");
if(isShorter(s1,s2))
cout<<"s1短!"<<endl;
else
cout<<"s2短!"<<endl;
string s("hello world");
int result5 = find_char(s,'o');
cout<<result5<<endl;
return 0;
}
003
#include<iostream>
using namespace std;
void ptrswap(int *&v1,int *&v2)
{
int *tmp = v2;
v2 = v1;
v1 = tmp;
}
int main()
{
int i = 10;
int j = 20;
int *pi = &i;
int *pj = &j;
cout<<i<<","<<j<<endl;
cout<<*pi<<","<<*pj<<endl;
ptrswap(pi,pj);
cout<<i<<","<<j<<endl;
cout<<*pi<<","<<*pj<<endl;
return 0;
}