使用vector的resize()函数过程中踩的坑

最近在学习写一些算法的代码用到了动态数组,主要思路是:在命令行中输入数组的大小,然后再调用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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章