2

I have an application in C++ VS2010.

I have a vector of integers

0=93823

1=987283

2=3183723

3=8892328

4=98382391

etc...

Is there a way to quickly serialize/deserialize this vector to a file in one rush (with the deserialization being cross platform) without having to write each value individually?

Thank you for the help.

Edit:

I am posting some code how I currently serialize and deserialize my vector. I feel that my way is highly inefficient.

void CCompiler::CompileVector(FILE *outfile)
{
    int iSize = nMap.Content().size();
    fwrite(&iSize,sizeof(int),1,outfile);

    for (int i=0;i<nMap.Content().size();i++)
    {
            fwrite( &nMap.Content()[i], sizeof(int), 1, outfile);
    }   
 }

void CBinLoader::LoadVector(clsMapping &uMapping)
{
    int iSizeMap = 0;
    fread(&iSizeMap,sizeof(int),1,m_infile);

    for(int i = 0; i < iSizeMap; i++)
    {
        int iByteStart=0;
        fread(&iByteStart,sizeof(int),1,m_infile);
        uMapping.Add(iByteStart);
    }
 }

ps: In my class clsMapping there are some voids. However, "Content" simply is a vector .

4

1 回答 1

1

不知道你在这个或你之前的问题中所说的“匆忙”是什么意思。

但是,由于它是文本,而不是二进制,这应该可以工作:

vector<int> values;

fstream file("filename");

copy(istream_iterator<int>(file),
     istream_iterator<int>(),
     back_inserter(values));
于 2013-05-11T06:20:23.243 回答