c++ 基础
#include <iostream>#include <vector>
#include<bitset>
#include <list>
#include <deque>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <memory>
#include<stdio.h>
#define min(a,b) ((a) < (b) ? (a) : (b))
using namespace std;
using std::bitset;
using std::string;
using std::vector;
//函数参数
void out(string *p) {//指针参数
cout << *p << endl;
*p = "k";
}
void out1(string p) {//值参数
cout << p << endl;
p = "k";
}
void out2(string &p) {//引用参数
cout << p << endl;
p = "k";
}
void out3(const string &p) {//只读引用参数
cout << p << endl;
}
//数组参数
void putValues1(int*);
void putValues2(int[]);
void putValues3(int[10]);
void putValues4(int[], int size);
void putValues5(const int[10]);
void putValues6(int (&ia)[10]);
void putValues7(vector<int>);
void putValues8(const vector<int> &);
void putValues(const vector<int> &);
//可变参数
void foo( ... ){
};
void foo1( int i, ... );
//默认值参数
char *screenInit(int height = 24, int width = 80, char background = ' ');
//函数返回值
int change(int i){
i++;
return i; //返回值
}
int& changepoint(int &m){
m++;
return m; //返回引用
}
int* changepoint1(int *m){
*m ++;
return m; //返回指针
}
//函数指针
void (*pfi)( string)=out1;
void (*testCases[3])(string)={out1,out1,out1};
typedef void (*PFV)(string);
PFV pfi1;
//PFV pfi2[3];
int sort( string*, string*, PFV );//函数作为参数声明
int sort( string *s1, string *s2,PFV compare = pfi1 ){//定义
//todo
}
// 复合语句形式的链接指示符,链接指示符不能出现在函数内
extern "C"{
void put( const char* a ... ){
};
int get( const char* b... );
}
//函数模板
template <class Type,class DType>
Type min1( Type a, DType b ) {
return a < b ? a : b;
}
template <class Type>
Type min2( Type a, Type b ) {
return a < b ? a : b;
}
template <class Type,int size>
Type min3(const Type (&r_array)[size]) {
/* 找到数组中元素最小值的参数化函数 */
Type min_val = r_array[0];
for ( int i = 1; i < size; ++i ){
if ( r_array[i] < min_val )
min_val = r_array[i];
}
return min_val;
}
template <class Type>
Type min3(const Type *r_array ,int size) {
/* 找到数组中元素最小值的参数化函数 */
Type min_val = r_array[0];
for ( int i = 1; i < size; ++i ){
if ( r_array[i] < min_val )
min_val = r_array[i];
}
return min_val;
}
typedef double Type;
template<class Type>
inline
Type min4(Type a, Type b) {
// 错误: 重新声明模板参数 Type
//typedef double Type;
// tmp 类型为模板参数 Type,如果在全局域中声明了与模板参数同名的对象函数或类型则该全局名将被隐藏
// 不是全局 typedef
Type tmp = a < b ? a : b;
return tmp;
}
template<class Type>
Type min5( Type a, int b){
Type tmp = a < b ? a : b;
return tmp;
}
//重载
template<class Type>
Type min5( Type a, Type b,int c){
Type tmp = a < b ? a : b;
return tmp;
}
// 错误: 必须是 <typename T, class U> 或 <typename T, typename U>
//template <typename T, U>
//T sum( T*, U );
template <class T1, class T2, class T3>
T1 sum( T2*, T3 );
int main() {
string s("hdfgl");
//cin >> s ;
//getline(cin,s);//读输入流
//string遍历
for (string::size_type i = 0; i < s.size(); i++) {
//cout << "!!!Hello World!!!" << s << endl; // prints !!!Hello World!!!
}
//vector操作
vector<int> ivc;
for (int i = 0; i < 10; i++) {
ivc.push_back(i);
}
for (vector<int>::size_type i = 0; i < ivc.size(); i++) {//遍历
//cout << ivc[i] << endl; // prints !!!Hello World!!!
}
vector<int>::iterator iter = ivc.begin();
for (; iter < ivc.end(); iter++) { //迭代器遍历
//cout << *iter;
}
vector<string> svec1(24, "pooh"); //初始化
svec1.resize(2 * svec1.size()); //重设大小,初始化
svec1.resize(2 * svec1.size(), "piglet");
vector<string> svec2(svec1);
vector<string> svec3(svec1.begin(), svec1.end());
svec2.insert(svec2.begin(),s); //插入
vector<int>::iterator viter;
viter = find( ivc.begin(), ivc.end(), 5 ); //寻找
cout<<*viter;
//bitset 操作
bitset<1> b = '0';
//cout << b[0] << endl;
//数组操作
int a[] = { 1, 2, 3 };
for (int i = 0; i < 3; i++) {
//cout<<a[i];
}
//list 操作
int sarray[4] = {7,8,9,10};
list<int> ilist;
const int list_size = 64;
list<int> ilist1(list_size);
int ia[4] = { 0, 1, 2, 3 };
for (int ix = 0; ix < 4; ++ix) {
ilist.push_front(ia[ix]);//头插入
ilist.push_back(ia[ix]);//尾插入
}
ilist.insert(ilist.begin(),1);//位置插入
ilist.insert( ilist.end(), 2 , 9 );
//ilist.insert( ilist.begin() + ilist.size()/2,sarray+2, sarray+4);
list<int> ilist2(list_size, -1);//初始化
list<int> ilist3(ilist);
ilist.pop_back();//弹出
ilist.erase( ilist.begin(), ilist.end() );
//map 操作
map<string, int> word_count;
word_count[ string("Anna") ] = 1;
typedef map<string,int>::value_type valType;//键值对定义
word_count.insert( valType(string("wrinkles"),1) );
map< string, int > word_count2; //初始化
map< string, int > word_count3( word_count );
word_count2.insert(word_count.begin(),word_count.end());//插入
int count;
if ( word_count.count( "Anna" ))//计数
count = word_count[ "Anna" ];
int count1 = 0;
map<string,int>::iterator it = word_count.find( "wrinkles" );//寻找
if ( it != word_count.end() )
count1 = (*it).second;
//cout<<count<<count1<<endl;
word_count.erase("wrinkles");//删除
for ( it = word_count.begin();it != word_count.end(); ++it )//遍历
cout << "key: " << (*it).first << "\t"<< "value: " << (*it).second << "\n";
//set 操作
set<string> exclusion_set;
exclusion_set.insert( "the" );//插入
set<string>::iterator itset = exclusion_set.find( "the" );//寻找
//cout<<*itset<<endl;
set< string >::iterator itset1=exclusion_set.begin();//遍历
for ( ; itset1 != exclusion_set.end(); ++itset1 ) {
//cout <<*itset;
}
//multimap multiset操作
multimap< string, int > authors;
typedef multimap< string,int >::value_type multype;//键值对定义
for (int i=0;i<10;i++){
authors.insert(multype("A",i));//插入
}
//authors.erase(authors.begin(), authors.end());//删除
string search_item( "A" );
int number = authors.count( search_item );//计数
multimap< string,int >::iterator iter1;
iter1 = authors.find( search_item );//寻找
for ( int cnt = 0; cnt < number; ++cnt, ++iter1 ){//遍历
//cout<<(*iter1).first<<(*iter1).second<<endl;
}
//stack 操作
stack< int > intStack;
for (int i=0 ; i < 10; ++i )
intStack.push( i );
while ( intStack.empty() == false )
{
int value = intStack.top();//取值
// cout<<value<<endl;
intStack.pop();//弹出
}
queue<int> q;
for (int i=0;i<10;i++){
q.push(i);
}
//cout<<"size:"<<q.size();
while(q.empty()==false){
int value = q.front();//头取值
//cout<<value<<endl;
q.pop();//弹出
}
//指针操作
//string* sp=&s;//初始化
//string* sp1=sp;
//cout << *sp<<endl;
//cout << *sp1<<endl;
// sp1=sp1+3;//计算
//cout<< *sp1<<sizeof(s)<<sizeof(*sp)<<endl;
//函数参数操作
// out(sp); //指针参数
// cout<<*sp;
//out1(s); //值参数
//cout<<s<<endl;
//out3(s); //引用参数
//out2(s);
//cout<<s;
pfi(s); //函数指针
testCases[2](s);//函数指针数组
//函数返回值
int ch=10;
int dd;
dd=change(ch);//返回值
//cout<<ch<<endl;
//cout<<dd<<endl;
dd=changepoint(ch);//返回引用
cout<<ch<<endl;
cout<<dd<<endl;
changepoint1(&ch);//返回指针
//cout<<ch<<endl;
//自动对象
int autoint=20;
cout<<autoint<<endl;
//动态分配的对象
int *k;
if(*k){
k= new int(1);
}
delete k;
//自动管理用new 表达式动态分配的单个对象
auto_ptr< int > pi( new int( 1024 ) );
auto_ptr< int > p2( new int( 2048 ) );
if ( *pi != 1024 )
cout<<"error";// 喔, 出错了
else *pi *= 2;
cout<<*pi<<" "<<*p2<<endl;
p2=pi;//pi所指的对象将自动被删除,拷贝给p2
cout<<"*pi"<<" "<<*p2<<endl;
auto_ptr< int > p_auto_int;
if(p_auto_int.get()==0)
p_auto_int.reset( new int( 10 ) );
cout<<*p_auto_int<<endl;
auto_ptr< string > pstr_auto( new string( "Brontosaurus" ) );//初始化
pstr_auto->assign( "Long-neck" );//从新设置
cout<<*pstr_auto<<endl;
if ( pstr_auto->empty() )
cout<<"error";
else
pstr_auto->size();
auto_ptr< string > pstr_auto2( pstr_auto.get() );//pstr_auto所指的对象将不会自动被删除,并且拷贝给p2
cout<<*pstr_auto<<" "<<*pstr_auto2<<endl;
auto_ptr< string > pstr_auto3( pstr_auto.release() );//pstr_auto所指的对象将自动被删除,拷贝给p2
cout<<"*pstr_auto"<<" "<<*pstr_auto3<<endl;
int count2=4;
// 分配单个int 型的对象,用 1024 初始化
int *pint = new int( 1024 );
// 分配一个含有1024 个元素的数组,未被初始化
int *pia = new int[ count2 ];
for ( int index = 0; index < count2; ++index )
pia[ index ] = 0;
delete [] pia;
// 分配一个含 4x1024 个元素的二维数组,未被初始化
//只有第一维可以用运行时刻计算的表达式来指定,其他维必须是在编译时刻已知的常量
int (*pia2)[ 1024 ] = new int[ count2 ][ 1024 ];
delete [] pia2;
const int *pci = new const int(1024);
delete pci;
//const int *pci = new const int[100]; // 错误不能初始化
//模板
cout<<min(1,2)<<endl;
cout<<min( 10.1, 20.0 )<<endl;
cout<<min1(1,2)<<endl;
cout<<min1( 10.1, 20 )<<endl;
cout<<min2(1,2)<<endl;
cout<<min2( 10.1, 20.1)<<endl;
int ar[3]={1,2,3};
int size = sizeof (ar) / sizeof (ar[0]);
cout<<min3(ar)<<endl;
cout<<min3(ar,size)<<endl;
cout<<min5(10,5)<<endl;;
typedef unsigned int ui_type;
char* cha="a";
int ui=1;
//ui_type local = sum< ui_type, char*, ui_type >(&cha, ui);
return 0;
}