【day5】今天对前50道实验题中的经典题目和思想小结一下

  • 1.递归思想
  • 2.编码思想
  • 3.memset的对字符数组初始化的方法总结
  • 4.模拟题中构造字符数组的正确初始化与结束符号的标记
  • 5.防止惯性思维,不要把自己的常识加到题目当中
  • 6.vector的另外一个强大函数功能补充erase()
  • 7.对string的直接修改是可以的
  • 8.关于迭代器的更深的认识,在使用erase函数之后的迭代器自动移后一位

3.memset对字符数组进行初始化的小结

不同于对于整型数组的初始化只能使用初始化为0或-1

对一个一维的字符数组进行初始化可以初始化为各种字符:

int n , m;
	cin >> n >> m;
	char seat[n * m];
	memset(seat , '1' , sizeof(seat));
	for(int i = 0 ;  i < n*m ; i++){
		cout << seat[i] << " "; 
	}
	memset(seat , '0' , sizeof(seat));
	for(int i = 0 ;  i < n*m ; i++){
		cout << seat[i] << " "; 
	}
	memset(seat , 'a' , sizeof(seat));
	for(int i = 0 ;  i < n*m ; i++){
		cout << seat[i] << " "; 
	}
	memset(seat , 'b' , sizeof(seat));
	for(int i = 0 ;  i < n*m ; i++){
		cout << seat[i] << " "; 
	}

对于二维数组:

	int n , m;
	cin >> n >> m;
	char seat[n][m];
	memset(seat , '1' , sizeof(seat));
	for(int i = 0 ;  i < n ; i++){
		for(int  j = 0 ; j < m ; j++){
		cout << seat[i][j] << " "; 
		}
		cout << endl;
	}

其他的也可以

4.模拟题中使用字符数组正确的顺序

				char temp[num+1];
				memset(temp ,'f', sizeof(temp));
				temp[num] = '\0';

就这一段模拟一个确定长度的字符串,必须按照这种顺序去写,否则会出问题

5.不要把自己的常识加到题目当中,有时候如果题目中有新的概念或者定义,一般都是反常识的

比如作业2中的日历问题,开始怎么提交都不对,反复修改还是不对,最后没办法问室友,室友告诉我

20 * 13 = 260 ,我之前一直觉得一年既然不是365天,那么就是是360天吧

这就说明了我被现实中获得的经验所束缚,明明环境已然改变,我却还是很固执地按照自己的办法去做事

最后,导致的结果就是不通过,这也说明今后真的需要好好地反思,可能自己的创新不够,突破不够,才会犯这样的错误

6.vector的另外一个强大函数功能补充erase()

这个功能在有些情况似乎是非常需要,比如内存分配类似的问题

Removes from the vector either a single element (position) or a range of elements ([first,last)).

This effectively reduces the container size by the number of elements removed, which are destroyed.

有两种用法

position

Iterator pointing to a single element to be removed from the vector.
Member types iterator and const_iterator are random access iterator types that point to elements.

使用迭代器直接删除某个元素

first, last

Iterators specifying a range within the vector] to be removed: [first,last). i.e., the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
Member types iterator and const_iterator are random access iterator types that point to elements.

使用迭代器删除两个迭代器中间的所有部分和第一个迭代器所指的元素

例子:删除某个元素:

#include <bits/stdc++.h>
using namespace std;
int main(){
	vector <int> vec;
	for(int i = 0 ; i < 10 ; i++){
		vec.push_back(i+1);
	}
//	for(int i = 0  ; i < 10 ; i++){
//		if(vec[i] == 4){
			vec.erase(vec[i]);
			vec.erase(&vec[i]);
			vec.erase(i);
			vec.erase(4);
//		}
//	}
	vector <int> :: iterator it;
	for(it = vec.begin() ; it != vec.end() ;){	
		if((*it) == 4){
			vec.erase(it);
		}
        else{
            it++;
        }
	}
	while(!vec.empty()){
		cout << vec.back() << " "; 
		vec.pop_back();
	}
	return 0;
}

(4已经被删除)

注释掉的部分是常见的错误的写法

注意这里的erase函数与string类函数的区别,因为string类函数除了使用迭代器删除某个元素和某段元素之外还有一种

传入两个int型参数start和length实现删除从start开始长度为length的一段的连续字符串的功能

示例:

删除某一段元素:

#include <bits/stdc++.h>
using namespace std;
int main(){
	vector <char> vec;
	for(int i = 0 ; i < 10 ; i++){
		vec.push_back((char)(i + 48));
	}
	vec.push_back('[');
	for(int i = 0 ; i < 10 ; i++){
		vec.push_back(i);
	}
	vec.push_back(']');
	for(int i = 9 ; i >= 0 ; i--){
		vec.push_back(i + 48);
	}
	int size = vec.size();
	for(int i = 0 ; i < size ; i++){
		cout << vec[i] << " ";
	}
	cout << endl;
	vector <char> :: iterator first , last , it;
	for(it = vec.begin() ; it != vec.end() ; it++){	
		if((*it) == '['){
			first = it;
		}
		else if((*it) == ']'){
			last = it;
		}
	}
	vec.erase(first,last + 1);//如果不加1,则后面的括号删不掉
	size = vec.size();
	for(int i = 0 ; i < size ; i++){
		cout << vec[i] << " ";
	}
	return 0;
}

这里最好玩的是这个字符里面有个闹铃字符,如果运行,会响一下

但是只会响一次,因为第二次已经被删掉了,就不会再响了

7.string类的修改没有一点问题

#include <bis/stdc++.h>
using namespace std;
int main(){
	string str = "123456789";
	str[0] = 1;
	str[2] = 1;
	str[8] = 1;
	cout << str;
	return 0;
}

8.每个容器的迭代器在使用erase操作之后会自动向后移动一位

比如对于vector的erase操作:

Return value of .erase() in vector

An iterator pointing to the new location of the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the sequence.

如果删除的不是最后一个元素,那么返回值为一个指向与当前被移除的元素的下一个元素的迭代器,否则就会返回指向.end()的迭代器

再比如对string的erase操作:

Return value of .erase() in string

sequence (1)
 string& erase (size_t pos = 0, size_t len = npos);
character (2)
iterator erase (const_iterator p);
range (3)
iterator erase (const_iterator first, const_iterator last);

The sequence version (1) returns *this.
The others return an iterator referring to the character that now occupies the position of the first character erased, or string::end if no such character exists.

第一种返回this指针(就是自己这种类型的变量,在这里是一个string,可以用一个新的string变量来接收),另外两种迭代器返回的也是迭代器,指向被删除的未知的新的元素(后面补上来的那个,相当于后移)

如果后面没有元素补上来,那么就返回一个指向.end()的迭代器

举例:

#include <bits/stdc++.h>
using namespace std;
int main(){
	string str = "123456789";
	string str1 = str.erase(0,2);
	cout << str << endl << str1;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值