c++ - 如何从源 vector <> 构建搜索结果的 vector <>?

标签 c++ c++11

考虑这个例子:

std::vector<Student> students;
//poplate students from a data source
std::vector<Student> searched(students.size());
auto s = std::copy_if(students.begin(), students.end(), searched.begin(),
    [](const Student &stud) {
        return stud.getFirstName().find("an") != std::string::npos;
    });
searched.resize(std::distance(searched.begin(), s));

我有以下问题:

  1. 是否可以为搜索到的等于初始 vector 的 vector 分配内存?可能有 500 个不小 对象,也许没有一个满足搜索条件?还有其他办法吗?
  2. 当复制到搜索到的 vector 时,它被称为复制赋值运算符,并且..很明显会生成一个拷贝。如果从这 500 个对象中有 400 个满足搜索条件怎么办? 不仅仅是内存浪费吗?

我是一个 C++ 菜鸟,所以我可能会说一些愚蠢的话。我不明白为什么要使用 vector<T>其中 T是一个对象。我会一直使用 vector<shared_ptr<T>> .如果T是像 int 这样的原始类型,我想使用它很简单 vector<T> .

我考虑这个示例是因为我认为它非常通用,您总是必须从数据库或 xml 文件或任何其他来源中提取一些数据。你有没有vector<T>在您的数据访问层或 vector<shared_ptr<T>>

最佳答案

关于你的第一个问题:

1 - Is it ok to allocate memory for searched vector equals to the initial vector? There may be 500 not small objects and maybe none satisfying the search criteria? Is there any other way?

您可以使用后插入器迭代器,使用 std::back_inserter() searched 创建一个的标准函数 vector :

#include <vector>
#include <string>
#include <algorithm>
#include <iterator> // This is the header to include for std::back_inserter()

// Just a dummy definition of your Student class,
// to make this example compile...
struct Student
{
    std::string getFirstName() const { return "hello"; }
};

int main()
{
    std::vector<Student> students;

    std::vector<Student> searched;
    //                   ^^^^^^^^^
    //                   Watch out: no parentheses here, or you will be
    //                   declaring a function accepting no arguments and
    //                   returning a std::vector<Student>

    auto s = std::copy_if(
        students.begin(),
        students.end(),
        std::back_inserter(searched),
    //  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    //  Returns an insert iterator
        [] (const Student &stud) 
        { 
            return stud.getFirstName().find("an") != std::string::npos; 
        });
}

关于你的第二个问题:

2 - When copying to the searched vector it is called the copy assignment operator and ..obviously a copy is made. What if from those 500 objects 400 satisfying the search criteria? Isn't just memory wasting?

嗯,如果您没有关于谓词选择性的统计信息,那么您就无能为力了。当然,如果您的目的是以某种方式处理某个谓词为真的所有学生,那么您应该使用 std::for_each()。在源 vector 上而不是创建一个单独的 vector :

std::for_each(students.begin(), students.end(), [] (const Student &stud) 
{ 
    if (stud.getFirstName().find("an") != std::string::npos)
    {
        // ...
    }
});

但是,这种方法是否满足您的要求取决于您的特定应用。

I don't see why to ever use vector<T> where T is a object. I would always use vector<shared_ptr<T>>.

是否使用(智能)指针而不是值取决于 whether or not you need reference semantics (除了关于复制和移动这些对象的可能的性能考虑之外)。根据您提供的信息,尚不清楚是否属于这种情况,因此这可能是个好主意,也可能不是。

关于c++ - 如何从源 vector <> 构建搜索结果的 vector <>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15303391/

相关文章:

c++ - 指向数组的指针未按预期工作

C++11 使用 pow() 和 std::complex 舍入错误

c++ - 对于(自动 e : elements) may cause one of elements to be wanted to be removed from vector

c++ - 如何打印 std::set of std::maps

c++ - 列表初始值设定项

python - 如何让我的 Python 扩展接受额外的参数?

c++ - 如何解析嵌套名称说明符?

c++ - 同一集合上的两个不同迭代器是否相等

c++ - 为什么 vector 访问运算符没有指定为 noexcept?

c++ - clang 能代替 Exuberant Ctags 吗?