星期五, 7月 16, 2010

C++ 雜筆記


最近剛好有朋友問了一個程式的小題目,
正好之前3月的問題,也和這個類似,
不過那時候沒時間,也 留到現在了。

1.cin 緩衝區問題:
參考:
http://hi.baidu.com/wingfy01/blog/item/b4b763b19b6ec8530923028a.html
http://hi.baidu.com/liuhaorancode/blog/item/7740ae1b5942381f8618bf09.html
code:
加入cin.ignore(1,' ');
str and str2 is char
    cin.get(str,999,'\n');
    cxList->scan(str);
    cin.ignore(1,' ');
    cin.get(str2,999,'\n');
    cxList2->scan(str2);

2.strtok() 使用
參考:
http://www.cnblogs.com/oomusou/archive/2009/05/10/c_strtok.html
http://www.cplusplus.com/reference/clibrary/cstring/strtok/
3.sstream stringstream 字串轉數字
參考:
http://topic.csdn.net/u/20090216/00/5cbfcce8-c9de-44b2-9909-cffcb6758410.html
http://www.csie.ntnu.edu.tw/~u91029/libtech.html
http://golineage.com/ShowSameTitleN/vc/29985.html
http://www.cplusplus.com/reference/iostream/stringstream/
4.使用vector
template < class T, class Allocator = allocator > class vector;
參考:
http://www.cplusplus.com/reference/stl/vector/
http://www.cnblogs.com/oomusou/archive/2008/03/22/1117686.html
code:
#include
....
class cx_List{+
.. ..
vector viValue;
void scan(char* str);
stringstream strStream;
.. ..
void
    cx_List::scan(char* str)
{
    char *cp;
    int i = 0,tmp = 0;
    //cout << str << endl;
    cp = strtok(str," ");
    while(cp != NULL)
    {
        //printf ("cp:%s\n",cp);
        this->strStream << cp;
        this->strStream >> tmp;
        //cout << tmp << endl;
        this->viValue.push_back(tmp);
        cp = strtok(NULL," ");
        this->strStream.clear();
        i++;
    }
}
};