c++ Expression: vector iterator + offset out of range

Today write code encountered this problem

void nextPermutation(vector<int>& nums) {
		int start = bigFlow(nums);
		sort(nums.begin() + start + 1, nums.end());  //报错行
	}

This function is given iterator bounds addition, because my bigFlow()function returns a value between -1 ~ n, so iterator interval should be between 1-n. That statement is a compiler error to calculate the numss.begin()+starttime if start=-1so, obviously iterator has been cross-border, in the absence of calculation +1before the compiler will report an error.
Correction: brackets nums.begin()+(start+1), or adjust the order of additionnums.begin()+1+start

Published 16 original articles · won praise 10 · views 4919

Guess you like

Origin blog.csdn.net/weixin_44826484/article/details/104649064