简体   繁体   中英

Difference between these two partition functions

I literally could not understand whether there is a or not between them. By the way the functions are functions used for . 功能。 The reason why I ask this is because I'm implementing a and with there first function below it does not work. 并且下面没有第一个函数。 Both written by but I simply cannot understand why the first one doesn't work

vector<int>partition(vector<int>vec,int start,int finish)
{
    vector<int>list;
    for(int i=start;i<finish;i++)
        list.push_back(vec[i]);
    return list;
}

vector<int>partition(vector<int>vec,int start,int finish)
{
    vector<int>parted;
    int size=finish-start+1;
    for(int i=0;i<size;i++)
        parted.push_back(vec[i+start]);
    return parted;
}

唯一的区别是,第一个将在输出中不包含vec[finish] ,而第二个将在输出中。

Seems like your just copying a range from the vector, you can use std::copy for that. Also for good practice, pass by const-reference when possible. And as other have pointed out the difference between the two is that the first function treats finish as one pass the end while the second one includes it. Generally you might want to stay consistent and use the first one since it would be consistent with STL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM