我需要一个STL算法,该算法需要一个谓词和一个集合,并在集合中true只有一个成员满足该谓词的情况下返回,否则返回false。
我将如何使用STL算法来做到这一点?
例如,用STL算法代码替换以下内容以表示相同的返回值。
int count = 0;
for( auto itr = c.begin(); itr != c.end(); ++itr ) {
if ( predicate( *itr ) ) {
if ( ++count > 1 ) {
break;
}
}
}
return 1 == count;
Run Code Online (Sandbox Code Playgroud) 标准C++库中的所有名称都是小写的,除了std::ios_base::Init.为什么是这样?
许多编译器提供128位整数类型,但我使用的那些都没有提供typedef int128_t.为什么?
据我记得,标准
int128_t为此目的储备intmax_t至少128位(并且,我不相信我已经使用了实际符合最后一点的实现)
截至目前,对流的异常支持非常糟糕.当Boost.System库被采用到C++ 11中时,给人的印象是可能会有异常改进.所有的变化所做的替换std::exception用std::system_error.虽然<system_error>开发人员本身就是一个很好的库,但标准委员会和标准库实现者并未采取任何措施来使用它来改进异常消息.
为了说明它有多糟糕,这里简要总结了下面的内容:
发生错误.
setstate用于设置badbit或failbit.
clear被称为setstate.
如果启用了异常,clear则会抛出异常ios_base::failure.
是的,这意味着对于所有错误,抛出相同的无用异常消息.这是在basic_ios级别指定的,因此所有派生类都会遇到此问题.违规报价:
[iostate.flags]/4 效果:如果
((state | (rdbuf() ? goodbit : badbit)) & exceptions()) == 0,返回.否则,该函数抛出一个fail类basic_ios::failure(27.5.3.1.1)的对象,使用实现定义的参数值构造.
以下是"实现定义的参数值"为我们提供的示例:
ios_base::clear: unspecified iostream_category error
Run Code Online (Sandbox Code Playgroud)
有一个简单的解决方案吗?
既不是Boost.Filesystem也不Boost.Iostreams是替代品<iostream>.前者是一个可以轻松处理文件系统的库(很可能出现在C++的下一个版本中),而后者与.sources和Sinks有关.该文档声明它ios_base::failure无论如何都要委托例外.Boost.Filesystem 确实提供<boost/filesystem/fstream.hpp>了使用path而不是const char*参数open().它显示了一个如何从标准库类继承的示例:
template …Run Code Online (Sandbox Code Playgroud) 假设我有一个向量:
std::vector<Foo> v;
Run Code Online (Sandbox Code Playgroud)
此向量已排序,因此相等的元素彼此相邻。
获得所有表示具有相等元素的范围的迭代器对的最佳方法是什么(使用标准库)?
while (v-is-not-processed) {
iterator b = <begin-of-next-range-of-equal-elements>;
iterator e = <end-of-next-range-of-equal-elements>;
for (iterator i=b; i!=e; ++i) {
// Do something with i
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何在上面的代码中获取b和的值e。
因此,例如,如果v包含以下数字:
index 0 1 2 3 4 5 6 7 8 9
value 2 2 2 4 6 6 7 7 7 8
Run Code Online (Sandbox Code Playgroud)
然后,我想在循环中具有b并e指向元素:
iteration b e
1st 0 3
2nd 3 4
3rd 4 6
4th 6 9
5th …Run Code Online (Sandbox Code Playgroud) 我想我从C++标准库遇到了std :: poisson_distribution的错误行为.
以下C++代码(文件poisson_test.cc)用于生成泊松分布数:
#include <array>
#include <cmath>
#include <iostream>
#include <random>
int main() {
// The problem turned out to be independent on the engine
std::mt19937_64 engine;
// Set fixed seed for easy reproducibility
// The problem turned out to be independent on seed
engine.seed(1);
std::poisson_distribution<int> distribution(157.17);
for (int i = 0; i < 1E8; i++) {
const int number = distribution(engine);
std::cout << number << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我按如下方式编译此代码:
clang++ -o poisson_test -std=c++11 poisson_test.cc …Run Code Online (Sandbox Code Playgroud) 我想了解std::reference_wrapper.
以下代码显示引用包装器的行为与引用完全不同.
#include <iostream>
#include <vector>
#include <functional>
int main()
{
std::vector<int> numbers = {1, 3, 0, -8, 5, 3, 1};
auto referenceWrapper = std::ref(numbers);
std::vector<int>& reference = numbers;
std::cout << reference[3] << std::endl;
std::cout << referenceWrapper.get()[3] << std::endl;
// I need to use get ^
// otherwise does not compile.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,隐式转换不适用于调用成员函数.这是一个固有的限制吗?我需要std::reference_wrapper::get经常使用吗?
另一个案例是这样的:
#include <iostream>
#include <functional>
int main()
{
int a = 3;
int b = 4;
auto refa = std::ref(a);
auto …Run Code Online (Sandbox Code Playgroud) 今晚我一直在看一些我过去几天一直在研究的代码,并开始阅读移动语义,特别是std :: move.我有几个问题要求专业人士确保我走正确的道路而不做任何愚蠢的假设!
首先:
1)最初,我的代码有一个返回大向量的函数:
template<class T> class MyObject
{
public:
std::vector<T> doSomething() const;
{
std::vector<T> theVector;
// produce/work with a vector right here
return(theVector);
}; // eo doSomething
}; // eo class MyObject
Run Code Online (Sandbox Code Playgroud)
鉴于"theVector"在这个和"扔掉"中是暂时的,我将该函数修改为:
std::vector<T>&& doSomething() const;
{
std::vector<T> theVector;
// produce/work with a vector right here
return(static_cast<std::vector<T>&&>(theVector));
}; // eo doSomething
Run Code Online (Sandbox Code Playgroud)
它是否正确?这样做有什么陷阱吗?
2)我在一个函数中注意到它返回std::string它自动调用移动构造函数.调试返回字符串(thankyou,Aragorn),我注意到它称为显式移动构造函数.为什么有一个字符串类而不是矢量?
我没有必要对此函数进行任何修改以利用移动语义:
// below, no need for std::string&& return value?
std::string AnyConverter::toString(const boost::any& _val) const
{
string ret;
// convert here
return(ret); // No …Run Code Online (Sandbox Code Playgroud) Gnu C++编译器似乎定义__cplusplus为1
#include <iostream>
int main() {
std::cout << __cplusplus << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这1在标准c ++模式下以gcc 打印,在C++ 0x模式下打印,使用gcc 4.3.4和gcc 4.7.0.
C++ 11 FDIS在"16.8预定义宏名[cpp.predefined]"中说
编译C++转换单元时,名称
__cplusplus定义为值201103L.(脚注:本标准的未来版本将以更大的值替换此宏的值.不合格的编译器应使用最多五位小数的值.)
旧的std C++ 03也有类似的规则.
GCC是否将此设置为1,因为它"不符合"?
通过阅读该列表,我认为__cplusplus如果我有一个支持C++ 11的编译器,我可以使用以便携方式检查.但是使用g ++这似乎不起作用.我知道...EXPERIMENTAL...宏,但很好奇为什么g ++以__cplusplus这种方式定义.
我最初的问题是在不同的空指针变体之间切换.像这样的东西:
#if __cplusplus > 201100L
# define MYNULL nullptr
#else
# define MYNULL NULL
#endif
Run Code Online (Sandbox Code Playgroud)
是否有一种简单且合理的便携式方式来实现这样的开关?
在本讲座中,发言者提到(一开始)标准库中没有纯虚函数(或者他没有注意到).我相信Alex Stepanov反对这种语言功能但是从最初的STL设计开始,是否有任何纯粹的虚拟内容进入标准库?
FWIW(并纠正我,如果我错了)唯一指针中的删除器最终在大多数实现中使用虚拟调度,但这些不是纯虚拟.
c++ virtual-functions pure-virtual c++-standard-library language-lawyer