小编qwe*_*rty的帖子

用std :: for_each改变对象是否可以?

for_each接受InputIterators:

//from c++ standard
template <class InputIterator, class Function>
   Function for_each (InputIterator first, InputIterator last, Function f);
Run Code Online (Sandbox Code Playgroud)

是否可以更改Function f中的对象,如下所示:

struct AddOne
{
    void operator()(int & x){x = x + 1;}
};

std::vector<int> vec(10);
std::for_each(vec.begin(),vec.end(),AddOne());
Run Code Online (Sandbox Code Playgroud)

此代码适用于VC++ 2008以及GCC,但它是否也是可移植(合法)代码?
(InputIterators仅保证可用作rvalue,在这种情况下,它们在AddOne的operator()中用作左值)

c++ stl

7
推荐指数
1
解决办法
2813
查看次数

性能的"家酿"STL?

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html

根据那篇文章,STL不适合游戏开发.你对此有何看法?

我目前的做法是:使用STL,如果导致性能问题与homebrew容器(或分配器)交换(还没有来,但我没有做高端的3D游戏;))

stl

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

分配给临时(例如5 = 10但用户定义的类型)

在为此链接做一些测试代码之后:
调用临时对象的方法是否安全?
我发现了c ++语言的一个相当奇怪的特性,我不确定它是如何工作的:

struct Test{
    int i;
    Test(int ii):i(ii){}
    Test& operator=(int ii){
        i = ii;
        return *this;
    }
    Test operator+(const Test& rhs){
        return Test(i + rhs.i);
    }
    void non_const_function(){
        i *= i;
    }
};

int main(){
        //code below gives errors, builtin types don't allow evil code
        //int i = 5+5 = 8;
        //int& iRef = 5+5;
        //int* iPtr = &(5+5);
        //5 = 10;

        Test x = Test(5) + Test(5) = 8;//assign to a temporary
        Test& xRef = Test(5) + …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
821
查看次数

标签 统计

c++ ×2

stl ×2