使用QStringList的QT lambda有错误:抛出'std :: bad_alloc'实例后调用终止 what():std :: bad_alloc
但是在c ++ STL中没有问题
我不为什么
//QT
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->tableWidget->setColumnCount(3);
ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"name"<<"sex"<<"age");
QVector<QString> nameList;
nameList<<"sam"<<"tom"<<"red"<<"lily"<<"tim"<<"sky";
QStringList sexList;
sexList<<"man"<<"man"<<"man"<<"women"<<"man"<<"woman";
ui->tableWidget->setRowCount(nameList.size());
for(int i=0;i<nameList.size();i++){
int k=0;
ui->tableWidget->setItem(i,k++,new QTableWidgetItem(nameList[i]));
ui->tableWidget->setItem(i,k++,new QTableWidgetItem(sexList[i]));
ui->tableWidget->setItem(i,k++,new QTableWidgetItem(QString::number(i+20)));
}
//这里的lambda用&引用
connect(ui->pushButton,&QPushButton::clicked,[&](){
bool flag = false;
QString zhao = "tiger";
for(int i=0;i<nameList.size();i++){
if(nameList[i] == zhao){
flag = true;
}
};
if(!flag){
nameList<<zhao;
ui->tableWidget->insertRow(0);
ui->tableWidget->setItem(0,0,new QTableWidgetItem(zhao));
ui->tableWidget->setItem(0,1,new QTableWidgetItem("mam"));
ui->tableWidget->setItem(0,2,new QTableWidgetItem("21"));
}
});
}
//c++ STL
int main()
{
vector<string> vs({ "sam", "tom" , "red" , "lily" , "tim" , "sky" });
string xiang = "tiger";
auto fun = [& ]() {
bool flag = false;
for (auto iter = vs.begin(); iter != vs.end(); iter++) {
if (*iter == xiang)
{
flag = true;
}
}
if (!flag) {
vs.push_back(xiang);
}
};
fun();
for (auto iter = vs.begin(); iter != vs.end(); iter++) {
cout << *iter << endl;
}
}
//没关系
抛出'std :: bad_alloc'实例后调用terminate what():std :: bad_alloc