我正在徘徊,如何在一条"简单"的行中使用单个命令来回避string包含在s中的内容.vectorfor_each
是的,我知道使用自定义仿函数很容易,但我不能接受,它不能用bind(至少我做不到).
#include <vector>
#include <string>
#include <algorithm>
std::vector<std::string> v;
v.push_back("abc");
v.push_back("12345");
std::for_each(v.begin(), v.end(), /*call std::reverse for each element*/);
Run Code Online (Sandbox Code Playgroud)
编辑:非常 感谢那些神奇的解决方案.但是,我的解决方案是不使用Visual Studio 2008功能包/ SP1附带的tr1 :: bind.我不知道为什么它不能像预期的那样起作用,但就是它的方式(即使MS承认它是有缺陷的).也许一些修补程序会有所帮助.
使用boost :: bind,一切都按照需要运行,并且非常简单(但有时候非常混乱:)).我真的应该首先尝试boost :: bind ...
当一个或两个输入容器是具有重复对象的多个集合时,算法std:set_union的返回是什么?重复丢失了吗?
我们假设例如:
multiset<int> ms1;
ms1.insert(1);
ms1.insert(1);
ms1.insert(1);
ms1.insert(2);
ms1.insert(3);
multiset<int> ms2;
ms2.insert(1);
ms2.insert(1);
ms2.insert(2);
ms2.insert(2);
ms2.insert(4);
vector<int> v(10);
set_union( ms1.begin(), ms1.end(), ms2.begin(), ms2.end(), v.begin() );
Run Code Online (Sandbox Code Playgroud)
输出会是什么?
我有一组shared_ptr,我想将remove_copy_if与谓词的自定义函数对象一起使用.我不知道"最好"的方式.现在,我有这个工作:
class CellInCol : public std::unary_function<const std::shared_ptr<Cell>,
bool>
{
public:
CellInCol( size_t col ) : _col( col ) {}
bool operator() ( const std::shared_ptr<Cell> &a ) const
{
return ( a->GetX() == _col );
}
private:
size_t _col;
};
typedef std::set<std::shared_ptr<Cell>, CellSorter> Container;
Container _grid;
// initialization omitted...
Puzzle::Container Puzzle::GetCol( size_t c )
{
Cell::Validate( c, 1, 9 );
Container col;
std::remove_copy_if( _grid.begin(), _grid.end(),
std::inserter( col, col.begin() ),
std::not1( CellInCol( c ) ) );
return col;
}
Run Code Online (Sandbox Code Playgroud)
我决定对shared_ptr执行const引用,因为该对象不会保留指针,这似乎比shared_ptr的额外副本更有效.
看起来对对象进行const引用似乎更好,但是我无法编译它.我把它改成了这个,但没有运气:
class …Run Code Online (Sandbox Code Playgroud) 我写了一些K-nearest-neighbor查询方法,它们构建了一个最接近给定查询点的点列表.为了维护该邻居列表,我使用std::priority_queue了top元素是查询点的最远邻居.这样我知道是否应该推送当前正在检查的新元素(如果距离当前最远的邻居的距离较小),并且当我的优先级队列具有多于K个元素时,可以弹出()最远的元素.
到目前为止,一切都很好.但是,当我输出元素时,我想从最接近最远的位置订购它们.目前,我只是从优先级队列中弹出所有元素并将它们放在输出容器上(通过迭代器),这会导致从最远到最近排序的一系列点,因此,我调用std::reverse输出迭代器范围.
举个简单的例子,这里是一个使用优先级队列的线性搜索(显然,我使用的实际最近邻查询方法要复杂得多):
template <typename DistanceValue,
typename ForwardIterator,
typename OutputIterator,
typename GetDistanceFunction,
typename CompareFunction>
inline
OutputIterator min_dist_linear_search(ForwardIterator first,
ForwardIterator last,
OutputIterator output_first,
GetDistanceFunction distance,
CompareFunction compare,
std::size_t max_neighbors = 1,
DistanceValue radius = std::numeric_limits<DistanceValue>::infinity()) {
if(first == last)
return output_first;
typedef std::priority_queue< std::pair<DistanceValue, ForwardIterator>,
std::vector< std::pair<DistanceValue, ForwardIterator> >,
detail::compare_pair_first<DistanceValue, ForwardIterator, CompareFunction> > PriorityQueue;
PriorityQueue output_queue = PriorityQueue(detail::compare_pair_first<DistanceValue, ForwardIterator, CompareFunction>(compare));
for(; first != last; ++first) {
DistanceValue d = distance(*first);
if(!compare(d, radius))
continue;
output_queue.push(std::pair<DistanceValue, ForwardIterator>(d, first));
while(output_queue.size() …Run Code Online (Sandbox Code Playgroud) 以下代码的行为与我的预期不符.请帮我理解它是如何工作的.
#include <algorithm>
#include <iterator>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
struct user
{
string name;
string age;
string id;
};
istream& operator>>(istream& is, user& s)
{
getline(is, s.name, ':');
getline(is, s.age, ':');
getline(is, s.id);
return is;
}
int main(int argc, char* argv[])
{
ifstream file("file.txt");
vector<user> vec;
copy(istream_iterator<user>(file), istream_iterator<user>(), back_inserter(vec));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的自定义运算符>>被调用两次,但我希望它只被调用一次因为内容是:
约翰:40:21-5821-0
我std::none_of使用i)for循环,ii)基于范围的for循环和iii)迭代器对三种不同的手动实现的性能进行了基准测试.令我惊讶的是,我发现虽然所有三个手动实现大致相同的时间,但std::none_of速度要快得多.我的问题是 - 为什么会这样?
我使用了谷歌基准测试库并编译了-std=c++14 -O3.运行测试时,我将进程的关联性限制为单个处理器.我使用GCC 6.2得到以下结果:
Benchmark Time CPU Iterations
--------------------------------------------------------
benchmarkSTL 28813 ns 28780 ns 24283
benchmarkManual 46203 ns 46191 ns 15063
benchmarkRange 48368 ns 48243 ns 16245
benchmarkIterator 44732 ns 44710 ns 15698
Run Code Online (Sandbox Code Playgroud)
在Clang 3.9上,虽然速度差较小,但std::none_of也比手动for循环快.这是测试代码(仅包括用于简洁的循环手册):
#include <algorithm>
#include <array>
#include <benchmark/benchmark.h>
#include <functional>
#include <random>
const size_t N = 100000;
const unsigned value = 31415926;
template<size_t N>
std::array<unsigned, N> generateData() {
std::mt19937 randomEngine(0);
std::array<unsigned, …Run Code Online (Sandbox Code Playgroud) 传递空容器的行为是否已std::lower_bound定义?
我检查了cppreference.com和我在网上找到的旧版C++标准,但找不到明确的答案.
该用于cppreference.com文档std::deque::erase了一句
在以下情况下,迭代器首先不需要可解除引用
first==last:删除空范围是无操作.
我想念这样的东西std::lower_bound以及其他算法.
我试图用std::remove_if它从一个简单的字符串中删除空格,但结果却很奇怪。有人可以帮我弄清楚发生了什么吗?
该代码是:
#include <iostream>
#include <algorithm>
#include <string>
int main(int argc, char * argv[])
{
std::string test = "a b";
std::remove_if(test.begin(), test.end(), isspace);
std::cout << "test : " << test << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望这只是打印出来:
test : ab
Run Code Online (Sandbox Code Playgroud)
但是我得到了
test : abb
Run Code Online (Sandbox Code Playgroud)
尝试另一个字符串,我得到:
输入:“ a bcde uv xy”
输出:“ abcdeuvxy xy”
似乎它在复制最后一个“单词”,但有时会添加一个空格。我怎样才能在不做任何奇怪的事情的情况下删除所有空间?
我写了一个我想声明为 const 的方法,但编译器抱怨。我追查了一下,发现是方法的这部分造成了困难:
bool ClassA::MethodA(int x)
{
bool y = false;
if(find(myList.begin(), myList.end(), x) != myList.end())
{
y = true;
}
return y;
}
Run Code Online (Sandbox Code Playgroud)
方法中发生的事情比这更多,但是在剥离所有其他内容后,这部分不允许该方法为 const。为什么stl find算法会阻止方法成为const?它是否以任何方式更改列表?
并行 STL 算法是否符合std::back_insert_iterator??
我可能误解了std::par和之间的区别std::par_vec,是否std::par_vec意味着需要预先分配输出范围?
代码示例:
auto numbers = {1,2,3,4,5,6};
auto squared = std::vector<int>{};
std::transform(
**std::par/std::par_vec,**
numbers.begin(),
numbers.end(),
std::back_inserter(squared),
[](auto val) {
return val*val;
}
);
Run Code Online (Sandbox Code Playgroud)
更新
简化问题作为我的第一个问题是误读文章的结果。
c++ ×10
stl-algorithm ×10
stl ×4
containers ×2
c++17 ×1
constants ×1
find ×1
ifstream ×1
iterator ×1
knn ×1
shared-ptr ×1
string ×1
tr1 ×1
vector ×1