简体   繁体   English

我需要对矢量进行排序吗? <int> 如果它们按顺序添加到向量中?

[英]Do I need to sort a vector<int> if they were added to the vector in order?

Do I need to sort my vector if the items were read in from a file sequentially that was already sorted? 如果从已经排序的文件中读入项目,是否需要对矢量进行排序? I don't want to incur the performance penalty if it's unnecessary. 如果不必要,我不想承担性能损失。 For reference, this is what I'm using to build it, and each item is in order as it's being read in. 作为参考,我正在使用它来构建它,并且每个项目在读入时都是有序的。

Edited after @dribeas' comment 在@dribeas的评论后编辑

std::vector<int> split(const std::string &s, char delim) {
    std::vector<int> elems;
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) {
        elems.push_back(static_cast<int>(atoi(item.c_str())));
    }
    return elems;
}

No, you don't need to sort the vector. 不,您不需要对矢量进行排序。 push_back adds elements to the end explicitly. push_back明确地向末尾添加元素。

Note that your API is taking in elems and also returning it (by reference). 请注意,您的API正在接受elems并返回它(通过引用)。 What happens to any content the vector already contains? 矢量已包含的任何内容会发生什么?

If you're asking whether a std::vector will preserve the order in which you push items onto it, then yes. 如果你问std::vector是否会保留你将项目推送到它上面的顺序,那么是的。 That is very much part of the contract. 这是合同的很大一部分。

Provided the vector was empty when you started (that's not clear from your original sample code) and the items were added in the order you want, no, you do not need to sort, because push_back pushes items on to the back (end) of the vector: 如果您在启动时向量为 (从原始示例代码中不清楚)并且项目按您想要的顺序添加,则不需要排序,因为push_back将项目推送到后面 (结束)向量:

Adds a new element at the end of the vector, after its current last element. 在向量的末尾添加一个新元素,在其当前的最后一个元素之后。


Following your edit, the possibility of the list being non-empty to start with is now gone. 在编辑之后,列表非空开始的可能性现在已经消失。 This section still stands for the original question where that was a possibility. 这部分仍然代表原始问题,这是一种可能性。

If the vector wasn't empty to begin with, the stuff that was in there may be out of order even if you added the new stuff in order. 如果向量不是空的,那么即使你按顺序添加了新的东西,那里的东西也可能出现故障。

If you want to ensure it's sorted, but avoid sorting unless absolutely necessary, you can do that as a post-step after the addition, something like: 如果你想确保它已经排序,但是除非绝对必要,否则你可以避免排序,你可以在添加之后作为后续步骤进行排序,例如:

std::vector<int> &split (const std::string &s, char sep, std::vector<int> &vec)
{
    std::stringstream ss (s);
    std::string item;
    while (std::getline (ss, item, sep))
        vec.push_back (static_cast<int> (atoi (item.c_str ())));

    bool sorted = true;
    for (int i = vec.size() - 1; (i > 0) && sorted; i--)
        sorted = (vec[i] >= vec[i-1]);
    if (!sorted) {
        // Sort it here.
    }

    return vec;
}

This will be a relatively fast scan of the elements to ascertain whether or not they're sorted (and only until it finds the first out of order group). 这将是对元素的相对快速的扫描,以确定它们是否被排序(并且直到它找到第一个无序组)。

If not sorted, it will sort them with your favourite sort algorithm (batteries not included), otherwise it will just return. 如果没有排序,它将使用您最喜欢的排序算法(不包括电池)对它们进行排序,否则它将返回。


However, it may still be best to assume that the data may not arrive in the order you want. 但是,它仍然可能是最好的假设数据可能不会在你想要的顺序到达。 You can still use a similar method to only sort when necessary, with something like: 您仍然可以使用类似的方法仅在必要时进行排序,例如:

std::vector<int> split (const std::string &s, char delim) {
    std::vector<int> elems;
    std::stringstream ss (s);
    std::string item;
    int lastOne = std::numeric_limits<int>::min ();
    bool isSorted = true;

    while (std::getline(ss, item, delim)) {
        int thisOne = atoi (item.c_str ());
        if (thisOne < lastOne) isSorted = false;
        elems.push_back (static_cast<int> (thisOne));
        lastOne = thisOne;
    }

    if (!isSorted) {
        // Sort it here.
    }

    return elems;
}

In that case, each element is checked against the last one to ensure it's at least as large. 在这种情况下,每个元素都会针对最后一个元素进行检查,以确保它至少同样大。 If not, the vector is flagged for sorting. 如果不是,则标记向量以进行排序。

With that, there's a minimal cost in checking but it gives you the ability to handle data that doesn't follow the rules. 有了它,检查的成本最低,但它使您能够处理不遵守规则的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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