8.5 看的东西

来源:互联网 发布:cs1.5枪械数据 编辑:程序博客网 时间:2024/04/28 16:59

 

void main(void)

{

string str = "12     ,2   ,4,6,21,78,23,";

string::size_type pos;

string temp;

vector<int>vec;

while(( pos = str.find(',')) != string::npos)

{

temp = str.substr( 0,pos );

cout<< temp <<endl;

vec.push_back(atoi(temp.c_str()));

str = str.substr(pos+1);

}

copy(vec.begin(),vec.end(),ostream_iterator<int>(cout,"/n"));

}

 

atoi 遇到空格转换结束。如 string str = “2   4”  atoi后就会变成 2.

要是 string str = “24   ”    atoi 后会变成 24.

 

Isip() 的判断

 

 

 

 

 

 

 

stringstream 的用法

转自:http://pingpeace.blog.51cto.com/304509/192887

stringstream用法(载)

2009-02-20 16:52

C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含sstream.h头文件。 

  istringstream类用于执行C++风格的串流的输入操作。 

  ostringstream类用于执行C风格的串流的输出操作。 

  strstream类同时可以支持C风格的串流的输入输出操作。

   istringstream类是从istream和stringstreambase派生而来,ostringstream是从ostream和stringstreambase派生而来, stringstream则是从iostream类和stringstreambase派生而来。

  他们的继承关系如下图所示:

 

  istringstream是由一个string对象构造而来,istringstream类从一个string对象读取字符。 

  istringstream的构造函数原形如下: 

  istringstream::istringstream(string str);

//程序作者:管宁 

//站点:www.cndev-lab.com 

//所有稿件均有版权,如要转载,请务必著名出处和作者 

#i nclude <iostream> 

#i nclude <sstream> 

using namespace std; 

int main()   

    istringstream istr; 

    istr.str("1 56.7",); 

    //上述两个过程可以简单写成 istringstream istr("1 56.7"); 

    cout << istr.str()<<endl; 

    int a; 

    float b; 

    istr>>a; 

    cout<<a<<endl; 

    istr>>b; 

    cout<<b<<endl; 

    system("pause"); 

}

  上例中,构造字符串流的时候,空格会成为字符串参数的内部分界,例子中对a,b对象的输入"赋值"操作证明了这一点,字符串的空格成为了整型数据与浮点型数据的分解点,利用分界获取的方法我们事实上完成了字符串到整型对象与浮点型对象的拆分转换过程。

  str()成员函数的使用可以让istringstream对象返回一个string字符串(例如本例中的输出操作(cout<<istr.str();)。

  ostringstream同样是由一个string对象构造而来,ostringstream类向一个string插入字符。 

  ostringstream的构造函数原形如下: 

  ostringstream::ostringstream(string str); 

 

  示例代码如下:

//程序作者:管宁 

//站点:www.cndev-lab.com 

//所有稿件均有版权,如要转载,请务必著名出处和作者 

#i nclude <iostream> 

#i nclude <sstream> 

#i nclude <string> 

using namespace std; 

int main()   

    ostringstream ostr; 

    //ostr.str("abc");//如果构造的时候设置了字符串参数,那么增长操作的时候不会从结        尾开始增加,而是修改原有数据,超出的部分增长 

    ostr.put('d'); 

    ostr.put('e'); 

    ostr<<"fg"; 

 

    string gstr = ostr.str(); 

    cout<<gstr; 

    system("pause"); 

}

   在上例代码中,我们通过put()或者左移操作符可以不断向ostr插入单个字符或者是字符串,通过str()函数返回增长过后的完整字符串数据,但值 得注意的一点是,当构造的时候对象内已经存在字符串数据的时候,那么增长操作的时候不会从结尾开始增加,而是修改原有数据,超出的部分增长。

[ basic_stringbuf::str : 

    Sets or gets the text in a string buffer without changing the write position. ]

  对于stringstream了来说,不用我多说,大家也已经知道它是用于C++风格的字符串的输入输出的。 

  stringstream的构造函数原形如下:

  stringstream::stringstream(string str);

  示例代码如下:

//程序作者:管宁 

//站点:www.cndev-lab.com 

//所有稿件均有版权,如要转载,请务必著名出处和作者 

#i nclude <iostream> 

#i nclude <sstream> 

#i nclude <string> 

using namespace std; 

 

int main()   

     stringstream ostr("ccc"); 

     ostr.put('d'); 

     ostr.put('e'); 

     ostr<<"fg"; 

    string gstr = ostr.str(); 

    cout<<gstr<<endl; 

 

    char a; 

     ostr>>a; 

    cout<<a 

 

     system("pause"); 

}

  除此而外,stringstream类的对象我们还常用它进行string与各种内置类型数据之间的转换。 

 

  示例代码如下:

//程序作者:管宁 

//站点:www.cndev-lab.com 

//所有稿件均有版权,如要转载,请务必著名出处和作者 

#i nclude <iostream> 

#i nclude <sstream> 

#i nclude <string> 

using namespace std; 

 

int main()   

     stringstream sstr; 

//--------int转string----------- 

    int a=100; 

    string str; 

     sstr<<a; 

     sstr>>str; 

    cout<<str<<endl; 

//--------string转char[]-------- 

     sstr.clear();//如果你想通过使用同一stringstream对象实现多种类型的转换,请注意在每一次转换之后都必须调用clear()成员函数。 

    string name = "colinguan"; 

    char cname[200]; 

     sstr<<name; 

     sstr>>cname; 

    cout<<cname; 

     system("pause"); 

 

/*

#include <iostream>

#include <cstdlib>

#include <new>

 

template <typename T>

T **darray_new(int row, int col)

{

    int size = sizeof(T);

    void **arr = (void **) malloc(sizeof(void *) * row + size * row * col);

    if (arr != NULL)

    {

        unsigned char * head;

        head = (unsigned char *) arr + sizeof(void *) * row;

        for (int i = 0; i < row; ++i)

        {

            arr[i] =  head + size * i * col;

            for (int j = 0; j < col; ++j)

                new (head + size * (i * col + j)) T;

        }

    }

    return (T**) arr;

}

 

template <typename T>

void darray_free(T **arr, int row, int col)

{

    for (int i = 0; i < row; ++i)

        for (int j = 0; j < col; ++j)

            arr[i][j].~T();

    if (arr != NULL)

        free((void **)arr);

}

*/

 

c++ 深拷贝、浅拷贝。回调函数、二维数组。

 

二维数组的实现:

1、是用vector 实现

2、A (*ga)[n] = new A[m][n];   

...   

delete []ga; 

 

 

 

 

 

 

2011    boost 库、一个平台的 api。汇编、python、设计模式。 stl。 (常用的)

 

 

RW

判断是否是IP

文件、写日志

配置文件读取

 

c接口设计与实现的  断言、内存、常用数据结构、字符串。

 

现在写程序 两个语言 一起用  c++、python。还是要写程序才能提高快。

 

假如要判断 一个字符串 是否是 IP     你要明白规则,才能写出来。

 

游戏编程感悟   c、c++ 部分c接口设计与实现

 

c++ primer 这5章看完 做一个总结。知识点 对应例子。

 

派生类构造函数  构造函数和复制控制成员 是不能复制的。

 

派生类对象 是由派生类定义的成员 加上一个或多个基类子对象构成。

 

class bulk_item:public: item_base

{

public:

bulk_item():min_qty(0),discount(0.0){}

 

}这个会调用默认构造函数。

 

派生类构造函数   会 先初始化 基类的构造函数、然后在构造派生类构造函数。

 

 

先运行初始化函数列表     在运行构造函数的函数体。

 

一个类 只能初始化 自己的直接基类。 

 

using add  是不是虚函数 都一样。

 

具有指针成员的派生类 需要自己定义复制构造函数。

 

 

下午:

例子:

派生类的复制控制成员:

派生类的赋值操作符。

 

 

class Derived : Base

{

Derived( const Derived& rhs):Base(rhs){}

Derived& operator = (const Derived& rhs)

{

if(this != rhs)

{

Base::operator=(rhs);

}

return * this;

}

}

 

 

虚函数是返回值 和形参 都一样。

打个比方:

class Base

{

public:

virtual int add(int n);

}

 

class Dervied:public Base

{

int add (int m); // 虚函数

int add (int m,int n); //  是在派生类中新加的,函数重载。是一个新的函数。

}

 

基类的指针 只能访问对象的基类部分。

501 页的名字查找与继承  说的比较明白。

 

面向对象编程还差 习题和文本查询程序。

 

复制控制 和 操作符重载 都是为了 继承与派生 准备的。

 

模板与泛型编程:

非类型模板形参.