我試圖將字符串添加到矢量字符串矢量的末尾,並以某種方式遇到了內存問題。將字符串添加到矢量的矢量
我的代碼與此類似
vector<vector<string>> slist;
....
slist.push_back(vector1);
slist.push_back(vector2);
...
for(int i=0; i<10; i++){
int length = slist.size()-1;
slist[length].push_back("String"); // also tried slist.back().push_back("S");
}
東西,這一些如何讓我有記憶問題
Invalid read of size 8
==2570== at 0x404D18: std::vector<std::string, std::allocator<std::string> >::push_back(std::string const&) (stl_vector.h:735)
==2570== by 0x403956: main (asm.cc:400)
==2570== Address 0xfffffffffffffff0 is not stack'd, malloc'd or (recently) free'd
==2570==
==2570==
==2570== Process terminating with default action of signal 11 (SIGSEGV)
==2570== Access not within mapped region at address 0xFFFFFFFFFFFFFFF0
==2570== at 0x404D18: std::vector<std::string, std::allocator<std::string> >::push_back(std::string const&) (stl_vector.h:735)
==2570== by 0x403956: main (asm.cc:400)
==2570==
誰能告訴我爲什麼?
PS:抱歉以前問不佳的問題..
'slist.back()'更好,但我們需要一個SSCCE。我不相信你已經展示了正確的代碼。 – chris
「內存問題」?什麼樣的記憶問題?你遇到了什麼錯誤?您向我們展示了我們不能用來重現問題的代碼,並表示您在未告知我們錯誤* *的情況下會得到一個錯誤。在這種情況下,我可以給出的最具體的答案是「可能有一種方法可以使代碼正常工作」 – jalf