| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 615 人关注过本帖, 1 人收藏
标题:c++ 基础
只看楼主 加入收藏
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
结帖率:40%
收藏(1)
已结贴  问题点数:20 回复次数:6 
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;
}

搜索更多相关主题的帖子: include memory include memory 
2014-07-28 13:27
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
#ifndef MAIN_H_
#define MAIN_H_

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <mysql.h>
#include "dbctrl.h"
using namespace std;

#endif /*MAIN_H_*/

int main4() {
    MYSQL mysql;
    MYSQL_RES *result;
    MYSQL_ROW row;

    mysql_init(&mysql);          // 初始化mysql结构

    if (!mysql_real_connect(&mysql, "localhost", "root", "123", "mybatis", 3309,
            NULL, 0)) {
        cout<<"数据库连接发生错误!"<<endl;
    } else {
        mysql_query(&mysql,"SET NAMES 'GBK'");
        cout<<"数据库连接成功!"<<endl;

        if (mysql_query(&mysql, "SELECT * FROM user")) {
            cout<<"数据库查询发生错误"<<endl;
        } else {
            cout<<"开始查询..."<<endl;

            result = mysql_store_result(&mysql);           // 获得结果集

            if (mysql_num_rows(result) != NULL) {
                int numRows = mysql_num_rows(result);         // 获得结果集中的记录数
                int numFields = mysql_num_fields(result);      // 获得表中字段数

                cout<<"共"<<numRows<<"行记录"<<","<<"每行"<<numFields<<"个字段"<<endl;
                while (row = mysql_fetch_row(result)) {
                    int i = 0;
                    for (i = 0; i < numFields; i++) {
                        fprintf(stdout, " %s", row[i]);     // 打印字段值
                    }
                    cout<<endl;
                }
            } else {
                cout<<"n无查询结果!"<<endl;
            }

            mysql_free_result(result);                    // 释放结果集
        }
    }

    mysql_close(&mysql);                                 // 释放数据库连接





    DBctrl my;              //刚才建的通用类
    string query;           //存放查询语句
    char*  data[100][100];  //存放查询结果

    query="SELECT * FROM user";
    my.DBconn("localhost","root","123","mybatis",3309);
    my.DBquery(query,data); //执行query中的语句,并将结果回送给data
    my.DBclose();           //关闭连接

    for(int i=0;i<my.row;i++){
        for (int j=0;j<my.field;j++){
            if(data[i][j])
              cout<<data[i][j];
        }
        cout<<endl;

    }

    /* 此处用for循环显示data数组的内容... */


    return 0;
}
2014-07-28 13:30
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
#include  <iostream>
#include<stdio.h>
using namespace std;
class A
{
    public:
        void Print()
        {
            printf("I am class A print\n");
        }
};
extern "C" A* method_1();
extern "C" void method_2(char* s);
2014-07-28 13:31
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
#include "head.h"
A* method_1()
{
    printf("I am method_1\n");
    return new A();
}

void method_2(char *s)
{
    printf("I am method_2 : %s\n",s);
}
2014-07-28 13:31
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
#include  "stdio.h"
#include  "stdlib.h"
//#include  "dlfcn.h"
#include "head.h"
typedef A* (*FunPtr)();
typedef void (*FunPtr2)(char*);
int main5()
{
    void  *SoLib;
    //typedef void (*FunPtr)();
    FunPtr So;
    FunPtr2 So2;
    SoLib=dlopen("./method.so",RTLD_LAZY);
    So   = (FunPtr)dlsym( SoLib, "method_1");
    A* a=(*So)();
    a->Print();
    So2    = (FunPtr2)dlsym(SoLib, "method_2");
    (*So2)("method_2");
}
2014-07-28 13:31
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
all:
    g++  method_1.cpp    -fPIC -shared -o method.so
    g++ work_so.cpp -o work_so  -ldl
clean:  
    rm method.so work_so
2014-07-28 13:32
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:20 
?

DO IT YOURSELF !
2014-07-28 13:41
快速回复:c++ 基础
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022528 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved