最近在學習寫一些算法的代碼用到了動態數組,主要思路是:在命令行中輸入數組的大小,然後再調用vector::resize()函數重新分配創建的vector對象的大小,從而實現動態數組的目標。但是在實踐中發現,這個resize()函數貌似工作不正常,代碼中如果寫了這個函數,代碼的輸出是沒有任何結果的。代碼如下(代碼是CCF201409-3 字符串匹配的答案,通過了用例測試):
/*******************
輸入樣例
Hello
1
5
HelloWorld
HiHiHelloHiHi
GrepIsAGreatTool
HELLO
HELLOisNOTHello
Hello
0
5
HelloWorld
HiHiHelloHiHi
GrepIsAGreatTool
HELLO
HELLOisNOTHello
輸出樣例
HelloWorld
HiHiHelloHiHi
HELLOisNOTHello
*******************/
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> strlist;
void converttosmall(string& str)
{
for(int i = 0 ; i < str.size(); ++i)
{
if(str[i] >= 'A' && str[i] <= 'Z')
{
str[i] -= ('A' -'a');
}
}
}
bool Judge(string& stri, string findstr, int issmart)
{
//string temp = str.
if(issmart == 0)//issmart爲0表示不區分大小寫
{
converttosmall(stri) ;
converttosmall(findstr);
}
else
{
;
}
if(stri.find(findstr) != string::npos )
{
return true;
}
else
{
return false;
}
}
int main()
{
string str, tempstr;
cin >> str;
int issmart = 0;//大小寫敏感選項
cin >> issmart;
int n = 0 ;
cin >> n;
// strlist.resize(n) ;
strlist.reserve(n+1);
for(int i= 0; i < n; ++i)
{
cin >> tempstr;
strlist.push_back(tempstr);
tempstr.clear();
}
cout << endl <<endl <<endl;
for(int i = 0; i < n ; ++i)
{
string currentstr = strlist[i];
if(Judge(currentstr, str, issmart) )
{
cout << strlist[i] << endl ;
}
}
return 0;
}
如上述所示,第80行處的resize()函數如果取消註釋,根據前面註釋中的用例輸入是沒有任何結果的。註釋掉resize()函數代碼能正常工作。前一段時間在做CCF的最優配餐問題我使用了resize()函數來創建動態數組,結果也是工作不正常。目前我能給出的思路就是對於需要創建的動態數組,可以寫一個for循環,循環中調用vector::push_back()函數來增加n個該類型的零值(即顯示調用構造函數,格式爲:類型名(), 如:int()就是創建一個int類型的零值)來達到我的目的(但是這樣會有效率上的損失,我的環境是DEVC++4.9.2,但是同樣的代碼在VS中也得不到結果。)。哪位看到這篇文章的大佬若有更好的解決方案,煩請告知一下,在此先謝過了。
更新於2019/6/18:在這一天我終於把該錯誤調試出來了。其實這個是因爲我的代碼中的push_back函數和resize函數之間的衝突導致的。調用resize函數之後,我向vector中填充數據的操作用的是push_back函數,這個函數是從vector的尾部添加數據,即使前面的0到n-1爲空,這個函數也只是從第n個(會自動重新分配內存)開始填充。而我所犯的錯誤是:理想當然的認爲前n個空間爲空,push_back函數應當從前n個開始填充。尷尬了(多謝宇哥的指點),下面是正確的代碼:
/*******************
輸入樣例
Hello
1
5
HelloWorld
HiHiHelloHiHi
GrepIsAGreatTool
HELLO
HELLOisNOTHello
Hello
0
5
HelloWorld
HiHiHelloHiHi
GrepIsAGreatTool
HELLO
HELLOisNOTHello
輸出樣例
HelloWorld
HiHiHelloHiHi
HELLOisNOTHello
*******************/
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> strlist;
void converttosmall(string& str)
{
for(int i = 0 ; i < str.size(); ++i)
{
if(str[i] >= 'A' && str[i] <= 'Z')
{
str[i] -= ('A' -'a');
}
}
}
bool Judge(string& stri, string findstr, int issmart)
{
//string temp = str.
if(issmart == 0)//issmart爲0表示不區分大小寫
{
converttosmall(stri) ;
converttosmall(findstr);
}
else
{
;
}
if(stri.find(findstr) != string::npos )
{
return true;
}
else
{
return false;
}
}
int main()
{
string str, tempstr;
cin >> str;
int issmart = 0;//大小寫敏感選項
cin >> issmart;
int n = 0 ;
cin >> n;
strlist.resize(n) ;
strlist.reserve(n+1);
for(int i= 0; i < n; ++i)
{
//cin >> tempstr;
//strlist.push_back(tempstr);
//strlist[i] = tempstr;
//tempstr.clear();
cin >> strlist[i];
cin.get();
}
cout << endl <<endl <<endl;
for(int i = 0; i < n ; ++i)
{
string currentstr = strlist[i];
if(Judge(currentstr, str, issmart) )
{
cout << strlist[i] << endl ;
}
}
return 0;
}