我在这段代码中有内存损坏:
#include <string>
#include <iostream>
#include <vector>
#include <initializer_list>
int main() {
std::vector<std::initializer_list<std::string>> lists = {
{
{"text1"},
{"text2"},
{"text3"}
},
{
{"text4"},
{"text5"}
}
};
int i = 0;
std::cout << "lists.size() = " << lists.size() << std::endl;
for ( auto& list: lists ) {
std::cout << "lists[" << i << "].size() = " << lists[i].size() << std::endl;
int j = 0;
for ( auto& string: list ) {
std::cout << "lists[" << i << "][" << j << "] = "<< string << std::endl;
j++;
}
i++;
}
}
样本输出:
lists.size() = 2
lists[0].size() = 3
lists[0][0] = text10�j ����text2H�j ����text3`�j ����text4����text5��������q
问题出在std::initializer_list
. 改成std::initializer_list
解决std::vector
问题。
问题是为什么会发生内存损坏std::initializer_list
?