모든 CopyMemory 함수를 std :: copy 함수로 변환하려고합니다.메모리를 std :: copy로 복사하십시오.
copymemory 및 memcpy에서는 작동하지만 std :: copy에서는 작동하지 않습니다. 누구든지 내가 뭘 잘못하고 어떻게 해결할 지 말해 줄 수 있니?
std::string Foo = "fdsgsdgs";
std::string::iterator it = Foo.begin();
unsigned char* pt = &(*it);
어떻게 다음 반복자 포인터를 변환합니다 :
template<typename T>
void S(unsigned char* &Destination, const T &Source)
{
//CopyMemory(Destination, &Source, sizeof(T));
std::copy(&Source, &Source + sizeof(T), Destination); //Fails..
Destination += sizeof(T);
}
template<typename T>
void D(T* &Destination, unsigned char* Source, size_t Size)
{
//CopyMemory(Destination, Source, Size);
std::copy(Source, Source + Size, Destination);
Source += sizeof(T);
}
template<typename T>
void D(T &Destination, unsigned char* Source, size_t Size)
{
//CopyMemory(&Destination, Source, Size);
std::copy(Source, Source + Size, &Destination);
Source += sizeof(T);
}
나는 또한 내가 할 수있는 포인터로 반복자를 변환하려면 다음을 냈어요? : S
내가 (그렇지 않은 경우 작동하는 경우 7 인쇄 ... 랜덤 숫자)을 방어 적이기/표준 대 copymem :: 다음과 같이 복사가됩니다를 테스트하는 데 사용하는 코드 :
#include <windows.h>
#include <iostream>
#include <vector>
#include <typeinfo>
using namespace std;
typedef struct
{
int SX, SY;
uint32_t Stride;
unsigned long ID;
int TriangleCount;
} Model;
template<typename T>
void S(unsigned char* &Destination, const T &Source)
{
CopyMemory(Destination, &Source, sizeof(T));
Destination += sizeof(T);
}
template<typename T>
void S(unsigned char* &Destination, const std::vector<T> &VectorContainer)
{
size_t Size = VectorContainer.size();
for (size_t I = 0; I < Size; ++I)
S(Destination, VectorContainer[I]);
}
void S(unsigned char* &Destination, const Model &M)
{
S(Destination, M.SX);
S(Destination, M.SY);
S(Destination, M.Stride);
S(Destination, M.ID);
S(Destination, M.TriangleCount);
}
template<typename T>
void D(T* &Destination, unsigned char* Source, size_t Size)
{
CopyMemory(Destination, Source, Size);
Source += sizeof(T);
}
template<typename T>
void D(T &Destination, unsigned char* Source, size_t Size)
{
CopyMemory(&Destination, Source, Size);
Source += sizeof(T);
}
template<typename T>
void D(std::vector<T> &Destination, unsigned char* Source, size_t Size)
{
Destination.resize(Size);
for(size_t I = 0; I < Size; ++I)
{
D(Destination[I], Source, sizeof(T));
Source += sizeof(T);
}
}
void D(Model* &Destination, unsigned char* Source)
{
D(Destination->SX, Source, sizeof(Destination->SX));
D(Destination->SY, Source, sizeof(Destination->SY));
D(Destination->Stride, Source, sizeof(Destination->Stride));
D(Destination->ID, Source, sizeof(Destination->ID));
D(Destination->TriangleCount, Source, sizeof(Destination->TriangleCount));
}
long double* LD = new long double[25000];
std::vector<Model> ListOfModels, ListOfData;
void ExecuteCommands()
{
switch(static_cast<int>(LD[1]))
{
case 1:
{
LD[2] = 2;
unsigned char* Data = reinterpret_cast<unsigned char*>(&LD[3]);
Model M; M.SX = 1; M.SY = 3; M.Stride = 24; M.ID = 7; M.TriangleCount = 9;
Model K; K.SX = 3; K.SY = 21; K.Stride = 34; K.ID = 9; K.TriangleCount = 28;
ListOfModels.push_back(M);
ListOfModels.push_back(K);
S(Data, ListOfModels);
}
break;
}
}
void* GetData()
{
unsigned char* Data = reinterpret_cast<unsigned char*>(&LD[3]);
D(ListOfData, Data, LD[2]);
cout<<ListOfData[0].ID; //Should print 7 if it works.
return &ListOfData[0];
}
int main()
{
LD[1] = 1;
ExecuteCommands();
GetData();
}
[RTFM] (http://www.cplusplus.com/reference/algorithm/copy/) : 대상은 출력 반복자 여야합니다. –
흠, 반복기가 없으므로 사용할 수 없습니다. : SI는 memcpy를 계속 사용합니다. – Brandon
오류가 발생했습니다. 당신이 소스 코드 또는 대상 타입을 templatize한다는 것만 제외하면 코드에 대한 유일한 괴상한 사실이 있습니다. 그러나 타입이 일치하지 않으면 std : : 복사본은 유형에 대해 안전합니다. – user1610015