顯示具有 C 標籤的文章。 顯示所有文章
顯示具有 C 標籤的文章。 顯示所有文章

2013年3月11日 星期一

[C++] STL iterator (泛型指標)

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
    vector  v;   //儲存一連串資料的陣列
    vector ::iterator iter;   //泛型指標,又叫做反覆器迭代器,主要是按照一定順序存取容器內容

    for(int i=1; i <= 10;i++)
        v.push_back(i);   //將1到10放入陣列

    for(iter = v.begin(); iter != v.end(); iter++) 
        cout << *iter <<" " ;
     cout << endl;

    return 0;
}

結果:



2013年3月8日 星期五

[C++] float to bytes[4]

方法一:
float f = 15.0f;

BYTE *pByte=(BYTE *)&f;

char tmp[64];
sprintf(tmp,"%x %x %x %x",pByte[0],pByte[1],pByte[2],pByte[3]);

cout << tmp << endl; 

方法二:
struct sFloat
{
   union
   {
         float mf;
         BYTE data[4];
   };
};

sFloat f;
char tmp[64];
 
f.mf=15.0f;
sprintf(tmp,"%x %x %x %x",f.data[0] ,f.data[1],f.data[2],f.data[3] ); 
cout << tmp << endl;

結果: