我需要浏览一个集合并删除符合预定义条件的元素.
这是我写的测试代码:
#include <set>
#include <algorithm>
void printElement(int value) {
std::cout << value << " ";
}
int main() {
int initNum[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::set<int> numbers(initNum, initNum + 10);
// print '0 1 2 3 4 5 6 7 8 9'
std::for_each(numbers.begin(), numbers.end(), printElement);
std::set<int>::iterator it = numbers.begin();
// iterate through the set and erase all even numbers
for (; it != numbers.end(); ++it) {
int n = *it; …Run Code Online (Sandbox Code Playgroud) 在C++中实现回调函数时,我是否还应该使用C风格的函数指针:
void (*callbackFunc)(int);
Run Code Online (Sandbox Code Playgroud)
或者我应该使用std :: function:
std::function< void(int) > callbackFunc;
Run Code Online (Sandbox Code Playgroud) 我正在制作一个需要彩色输出的简单应用程序.如何让我的输出像emacs和bash一样变色?
我不关心Windows,因为我的应用程序仅适用于UNIX系统.
C在某种程度上并不完全是C++的一个子集.因此,我们可以通过更改名称(stdio.hto cstdio,stdlib.hto cstdlib)来使用C++中的大多数C函数/头文件.
我的问题实际上是一种语义.在C++代码(使用GCC编译器的最新版本),我可以打电话printf("Hello world!");,并std::printf("Hello world!");和它的工作原理完全一样.在我使用它的参考中也显示为std::printf("Hello world!");.
我的问题是,它是否更适合std::printf();在C++中使用?有区别吗?
在阅读如何使用std :: rand时,我在cppreference.com上找到了这段代码
int x = 7;
while(x > 6)
x = 1 + std::rand()/((RAND_MAX + 1u)/6); // Note: 1+rand()%6 is biased
Run Code Online (Sandbox Code Playgroud)
右边的表达有什么问题?尝试过,它完美无缺.
我发现自己刚刚写了这篇文章:
template <long int T_begin, long int T_end>
class range_class {
public:
class iterator {
friend class range_class;
public:
long int operator *() const { return i_; }
const iterator &operator ++() { ++i_; return *this; }
iterator operator ++(int) { iterator copy(*this); ++i_; return copy; }
bool operator ==(const iterator &other) const { return i_ == other.i_; }
bool operator !=(const iterator &other) const { return i_ != other.i_; }
protected:
iterator(long int start) : i_ (start) { } …Run Code Online (Sandbox Code Playgroud) 我目前有一个std::map<std::string,int>存储整数值到唯一字符串标识符,我确实查找字符串.它主要是我想要的,除了它不跟踪插入顺序.因此,当我迭代地图以打印出值时,它们将根据字符串进行排序; 但是我希望它们按照(第一次)插入的顺序排序.
我想过使用一个vector<pair<string,int>>替代,但我需要查找字符串并将整数值增加大约10,000,000次,所以我不知道是否std::vector会明显变慢.
有没有办法使用std::map或是否有std更适合我需要的容器?
[我在GCC 3.4上,我的价值可能不超过50对std::map].
谢谢.
#include <iostream>
#include <cmath>
/* Intentionally incorrect abs() which seems to override std::abs() */
int abs(int a) {
return a > 0? -a : a;
}
int main() {
int a = abs(-5);
int b = std::abs(-5);
std::cout<< a << std::endl << b << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期望输出将是-5和5,但输出是-5和-5.
我想知道为什么会发生这种情况?
它与使用std或什么有关?
std::initializer_list内置核心语言?在我看来,它是C++ 11的一个非常重要的特性,但它没有自己的保留关键字(或类似的东西).
相反,initializer_list它只是来自标准库的模板类,它具有来自编译器处理的新braced-init-list语法的特殊隐式映射. {...}
起初认为,这个解决方案非常黑客.
这是现在实现C++语言新增功能的方式:通过某些模板类的隐式角色而不是核心语言?
请考虑以下示例:
widget<int> w = {1,2,3}; //this is how we want to use a class
Run Code Online (Sandbox Code Playgroud)
为什么选择了新课程:
widget( std::initializer_list<T> init )
Run Code Online (Sandbox Code Playgroud)
而不是使用类似于以下任何想法的东西:
widget( T[] init, int length ) // (1)
widget( T... init ) // (2)
widget( std::vector<T> init ) // (3)
Run Code Online (Sandbox Code Playgroud)
const这里和那里添加const和&所有这些都已经成为语言的一部分.我只写了我的第一个想法,我相信还有很多其他方法.