这是我的代码。 的 EDITED
#include<iostream>
#include<vector>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<string> v;
for(int i=0;i<n;i++)
{
string temp;
cin>>temp;
v.push_back(temp);
//if(v.size()==1){
// continue;
//}
//cout<<i<<endl;
// cout<<endl;
//cout<<v.size();
int k=i;
while(v[k-1].length()>v[k].length()&&k>-1)
{
swap(v[k],v[k-1]);
k--;
}
// cout<<endl;
}
bool check=true;
for(int i=0;i<v.size()-1;i++)
{
//cout<<v[i]<<endl;
//cout<<v[i+1]<<endl;
if (v[i+1].find(v[i]) != std::string::npos) {
//std::cout << "found!" << '\n';
continue;
}
// cout<<"false"<<endl;
check=false;
}
if(check==true)
{
cout<<"YES"<<endl;
for(int i=0;i<n;i++)
{
cout<<v[i]<<endl;
}
}else{
cout<<"NO"<<endl;
}
}
出现此错误的原因是什么: 输入是:
100
npugmvzdgfnzyxuyfwbzwktiylhvhwgeqauolidpnbemhgbunpefzsltewkxdcrzxgvmkb
bezfumiguzafxghvcfqmwpopxvazctlftelveayycypjckooxeehyk
ingenqhogs
elhnhxjwrytbmmqdwwrivvljybhnwfgwhvdgjqgqgvunuemdtrgpyvaanovheqbupamzrjxh
rpvktlmyxfshahfgunrhuqtosysymfjruqlzdooauuihtchzqgyrhcoxbtoorkxkwakvdkiakitlqfbgz
tnrnpghjmqumbzfnztiijgwkiygyfevfebuammkwnoinqvhhlsuoqtfkazqhlnuqtthudhhovjqiuykwqtck
mloehzniuwyakgwmopfgknpoiuiyewijmoefjjjsdimkisugehwqefcx
tthmaxtahimxxts
fspoetalxgcgowhjtanerjpqnen
hefsyokneekdgpbicss
适用于其他情况。
Diagnostics detected issues [cpp.clang++-diagnose]: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\vector:1802:11: runtime error: addition of unsigned offset to 0x129000a0 overflowed to 0x12900088
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\vector:1802:11 in
答案 0 :(得分:2)
出现此错误的原因是什么:
您将i设为0
int i=0;
然后你将k设置为i
int k=i;
然后索引v。
的 -1元素while(v[k-1].length()
No bounds checking is performed with std::vector:>:operator[]
你最终会把你不拥有的记忆当作一个有效构建的std::string
处理。