标签: stl-algorithm

是跨平台和性能的c ++ STL算法和容器?

在学习了大量的c ++之后,我现在进入了STL容器和算法模板库,我的主要关注点是,

1)这个库是否在MS,linux等其他平台上的不同平台上相同?

2)程序c ++模块的质量或效率会随着STL容器和算法的使用而减少,我想我无法根据所有需求对其进行定制.

3)这个模板库是否适合在linux系统编程,内核模块中使用?

4)最后我可以在编程竞赛中使用它,因为它重复了大量的编码和压力.

c++ stl stl-algorithm

4
推荐指数
1
解决办法
439
查看次数

在shared_ptr的容器上使用C++ std :: equal

我有一个std :: shared_ptr的容器.我想使用std :: equal来比较两个容器.A类有operator == defined.我希望等于比较每个元素是否等效使用其运算符==,而不是在shared_ptr中定义的元素.

我是否需要使函数或函数对象传递给相等的?或者是否有更简单的内置内容(如<functional>中定义的内容)?

c++ equality shared-ptr stl-algorithm

4
推荐指数
2
解决办法
2490
查看次数

std :: sort在std:指针向量上失败

以下代码在对矢量进行排序时崩溃.

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

struct Foo
{
    int x;
    // int y;
    Foo() : x(0) {}
};

struct Cmp
{
    bool operator() (Foo* p1, Foo *p2) const
    {
        if (p1->x != p2->x) return p1->x < p2->x;
        // if (p1->y != p2->y) return p1->y < p2->y;
        return true;
    }
};

int main()
{
    vector<Foo*> v;
    for (int i=0; i<17; i++) // weird thing, doesn't crash if
                             // I put a number less than 17 !!!
    {
        Foo *ptr …
Run Code Online (Sandbox Code Playgroud)

c++ sorting stl vector stl-algorithm

4
推荐指数
1
解决办法
2887
查看次数

std :: next_permutation实现解释看似效率不高?

我很好奇std:next_permutation是如何实现的所以我提取了gnu libstdc ++ 4.7版本并清理了标识符和格式以生成以下演示...

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

template<typename It>
bool next_permutation(It begin, It end)
{
    if (begin == end)
            return false;

    It i = begin;
    ++i;
    if (i == end)
            return false;

    i = end;
    --i;

    while (true)
    {
            It j = i;
            --i;

            if (*i < *j)
            {
                    It k = end;

                    while (!(*i < *--k))
                            /* pass */;

                    iter_swap(i, k);
                    reverse(j, end);
                    return true;
            }

            if (i == begin)
            {
                    reverse(begin, end); …
Run Code Online (Sandbox Code Playgroud)

c++ performance permutation stl-algorithm

4
推荐指数
1
解决办法
434
查看次数

从另一个容器创建容器,在c ++中应用每个元素一些函数

我的问题很简单,参见示例:

std::array<int,6> a = {{0,1,2,3,4,5}}; // -- given container.
auto F = []( int i ) { return  i*i; }; // -- given function.

std::vector<int> v;  // need create

// my solution:
v.reserve( a.size () );
for( std::size_t i = 0; i < a.size(); ++i )
    v.push_back( F(a[i]) ); 



// but I need something like
   std::vector<int>v( a.begin(), a.end(), <|applying each element to F|> );
Run Code Online (Sandbox Code Playgroud)

我可以创建上面的容器,而不是显式调用reserve和任何重新分配吗?

编辑:

  1. 我想避免储备; 因为其他我的第一个解决方案对我有好处:)
  2. 我想避免任何调整大小; 因为默认情况下它会初始化每个元素.
  3. 我将在真实项目中使用它,其中可能包括许多第三方库(boost,Soft-STL,...).

c++ containers iterator stl-algorithm c++11

4
推荐指数
3
解决办法
135
查看次数

是否有替代插入然后排序

如果我有,vector<int> foo并且vector<int> bar两者都已排序,并且我想将它们合并为foo最终结果已排序,那么标准是否为我提供了这样做的方法?

显然我可以这样做:

foo.insert(foo.end(), bar.begin(), bar.end());
sort(foo.begin(), foo.end());
Run Code Online (Sandbox Code Playgroud)

但我希望有一个步骤算法来实现这一目标.

c++ sorting merge insert stl-algorithm

4
推荐指数
2
解决办法
543
查看次数

删除所有未找到的,即删除在集合中找不到的地图中的所有键/值

我试过但未能得到以下工作std::algorithms:我有aa std::map<key_t,value_t> cache和a std::set<key_t> selected_items我想删除键/值对cache,除了包含的键selected_items.

这是我没有算法写的东西:

//This could really be written better with std::algorithms but time...
//Delete old
for (auto pair = cache.begin(); pair != cache.end(); ) {
    if (selected_items.find(pair->first) == selected_items.end())
        pair = cache.erase(pair);
    else
        ++pair;
}
Run Code Online (Sandbox Code Playgroud)

为了利用算法库,我想我需要使用std::set_difference比较函数和任何一个std::removestd::map::erase.但我不能连接件,失败的是:

  1. 什么是正确的比较功能?
  2. 我是否必须使用应删除的键生成临时集,还是可以直接使用输出迭代器进行删除/擦除?

我的代码应该怎么样?

c++ stl-algorithm c++11

4
推荐指数
1
解决办法
141
查看次数

为什么需要std :: minmax_element?

为什么我们需要的组合版本std::min_element,并std::max_elementstd::minmax_element?它只是用于保存比较或其背后的其他好处吗?

c++ stl-algorithm

4
推荐指数
1
解决办法
213
查看次数

std :: copy_n不会更改目标向量大小

如果我为一个向量保留了一些空间,然后我用它复制了一些值std::copy_n(),我得到正确复制并可访问的值,但向量的大小仍为零.这是预期的行为吗?我应该调整矢量大小,即使它没有效率吗?

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<double> src, dest;

    for(double x = 0.0; x < 100.0; ++x)
        src.push_back(x);

    dest.reserve(src.size());

    std::copy_n(src.cbegin(), src.size(), dest.begin());

    std::cout << "src.size() = " << src.size() << std::endl;
    std::cout << "dest.size() = " << dest.size() << std::endl;

    for(size_t i = 0; i < src.size(); ++i)
        std::cout << dest[i] << "  ";

}
Run Code Online (Sandbox Code Playgroud)

编译器测试:clang,gcc,Visual C++

c++ copy stdvector stl-algorithm c++11

4
推荐指数
1
解决办法
573
查看次数

范围v3使序列变平

所以我最近在c ++上看了这个演讲:https: //www.youtube.com/watch?v = mFUXNMfaciE

我对尝试它非常感兴趣.因此,在一些玩具程序之后,我被困在如何正确地将矢量矢量平面化为矢量.根据这里的文档:https://ericniebler.github.io/range-v3/这是可能的使用ranges::view::for_each.但是我似乎无法让它发挥作用.这是一些最小的代码.

#include <range/v3/all.hpp>
#include <iostream>
#include <vector>

int main()
{
    auto nums = std::vector<std::vector<int>>{
        {0, 1, 2, 3},
        {5, 6, 7, 8},
        {10, 20},
        {30},
        {55}
    };

    auto filtered = nums
        | ranges::view::for_each([](std::vector<int> num) { return ranges::yield_from(num); })
        | ranges::view::remove_if([](int i) { return i % 2 == 1; })
        | ranges::view::transform([](int i) { return std::to_string(i); });

    for (const auto i : filtered)
    {
        std::cout << i << …
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl-algorithm range-v3

4
推荐指数
1
解决办法
1118
查看次数