在学习了大量的c ++之后,我现在进入了STL容器和算法模板库,我的主要关注点是,
1)这个库是否在MS,linux等其他平台上的不同平台上相同?
2)程序c ++模块的质量或效率会随着STL容器和算法的使用而减少,我想我无法根据所有需求对其进行定制.
3)这个模板库是否适合在linux系统编程,内核模块中使用?
4)最后我可以在编程竞赛中使用它,因为它重复了大量的编码和压力.
我有一个std :: shared_ptr的容器.我想使用std :: equal来比较两个容器.A类有operator == defined.我希望等于比较每个元素是否等效使用其运算符==,而不是在shared_ptr中定义的元素.
我是否需要使函数或函数对象传递给相等的?或者是否有更简单的内置内容(如<functional>中定义的内容)?
以下代码在对矢量进行排序时崩溃.
#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) 我很好奇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) 我的问题很简单,参见示例:
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和任何重新分配吗?
编辑:
如果我有,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)
但我希望有一个步骤算法来实现这一目标.
我试过但未能得到以下工作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::remove或std::map::erase.但我不能连接件,失败的是:
我的代码应该怎么样?
为什么我们需要的组合版本std::min_element,并std::max_element为std::minmax_element?它只是用于保存比较或其背后的其他好处吗?
如果我为一个向量保留了一些空间,然后我用它复制了一些值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 ++上看了这个演讲: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++ ×10
stl-algorithm ×10
c++11 ×3
iterator ×2
sorting ×2
stl ×2
containers ×1
copy ×1
equality ×1
insert ×1
merge ×1
performance ×1
permutation ×1
range-v3 ×1
shared-ptr ×1
stdvector ×1
vector ×1