मुझे मध्य प्रविष्टि से जुड़े निश्चित सरणी में डेटा में हेरफेर करने की आवश्यकता है। memcpy, आदि का उपयोग करने के बजाय। मैं वेक्टर का उपयोग करना चाहता हूँ। जब मुझे वेक्टर तत्वों को वापस सी-शैली सरणी में कॉपी करने के लिए चाहिए तो मुझे समस्या है।std :: vector की सामग्री को सी-स्टाइल स्थिर सरणी में सुरक्षित रूप से कैसे कॉपी करें?
void tryvector()
{
using namespace std;
const int MAX_SIZE=16;
BYTE myarr[MAX_SIZE]={0xb0,0x45,0x47,0xba,0x11,0x12, 0x4e};
vector<BYTE> myvec (myarr, myarr+MAX_SIZE);
vector<BYTE>::iterator it;
printf("myarr pre :");
for(int i=0;i<MAX_SIZE;++i){
printf("%02x ", myarr[i]) ;
}
printf("\nmyvec pre :")
for(it=myvec.begin(); it<myvec.end();++it){
cout<<hex<<static_cast<int>(*it)<<" ";
}
it = myvec.begin()+ 3;
myvec.insert(it,0x5f);
printf("\nmyvec post:");
for(it=myvec.begin(); it<myvec.end();++it){
cout<<hex<<static_cast<int>(*it)<<" ";
}
copy(myvec.begin(), myvec.end(), myarr); //???
printf("\nmyarr post:");
for(int i=0;i<MAX_SIZE;++i){
printf("%02x ", myarr[i]) ;
}
}
मैं बनाम 2005 यहाँ का उपयोग कर रहा है चेतावनी: कोड यह
warning C4996: 'std::_Copy_opt' was declared deprecated
1> c:\program files\microsoft visual studio 8\vc\include\xutility(2270) : see declaration of 'std::_Copy_opt'
1> Message: 'You have used a std:: construct that is not safe. See documentation on how to use the Safe Standard C++ Library'
1> c:\documents and settings\mhd\my documents\tesvector.cpp(50) : see reference to function template instantiation '_OutIt std::copy<std::_Vector_iterator<_Ty,_Alloc>,BYTE*>(_InIt,_InIt,_OutIt)' being compiled
1> with
1> [
1> _OutIt=BYTE *,
1> _Ty=BYTE,
1> _Alloc=std::allocator<BYTE>,
1> _InIt=std::_Vector_iterator<BYTE,std::allocator<BYTE>>
1> ]
जब मैंने इसे चलाने के लिए, मैं निम्नलिखित रन-टाइम त्रुटि मिला:
Run-Time Check Failure #2 - Stack around the variable 'myarr' was corrupted.
कृपया ध्यान दें कि मैं वेक्टर के बजाय सूची या डेक का उपयोग करता हूं क्योंकि उपरोक्त कोड की तरह 'मध्य सम्मिलन' एक विशेष जांच है एलईएम। यह 'अंत में डालने' और 'तत्व की यादृच्छिक पहुंच' से कम होगा।
कोई समाधान?
कोई भी उत्तर जैसा दिखता है: "आप सी ++ का उपयोग करते हैं, सी शैली सरणी कार्यान्वयन ड्रॉप करें। सभी सरणी कार्यान्वयन के लिए केवल वेक्टर का उपयोग करें" वास्तव में सहायक नहीं है।
धन्यवाद।
सिर्फ इसलिए कि आप एक सरणी के बजाय वेक्टर <> का उपयोग कर रहे हैं इसका मतलब यह नहीं है कि memcpy() नहीं हो रहा है। वेक्टर <> विधियों को समझना आसान होता है, लेकिन वही काम करते हैं, और प्रदर्शन बड़े सरणी के लिए खराब होगा। – slacy