小编Unc*_*ens的帖子

C++ 0x中的完美转发是否会使reference_wrapper弃用?

像往常一样,代码优先:

#include <functional>

using namespace std;
using namespace std::tr1;

void f(int& r) { r++; }

template<class F, class P> void g1(F f, P t) { f(t); }
template<class F, class P> void g2(F f, P&& t) { f(forward<P>(t)); }

int main()
{
    int i = 0;

    g1(f, ref(i)); // old way, ugly way
    g2(f, i); // new way, elegant way
}
Run Code Online (Sandbox Code Playgroud)

在C++ 98中,我们没有一种通过模板函数来完善前向参数的好方法.所以C++大师发明了ref和cref来实现这个目标.

现在我们已经有了r值参考和完美转发,是否应该弃用ref和cref等?

c++ c++11

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

重载operator =里面的模板类

template<typename T> ClassA
{
    ...
    ClassA& operator=(const ClassA&);

    ...
};

// case one:
template<typename T>
ClassA<T>& ClassA<T>::operator=(const ClassA &rhs)
{ ... }

// case two:
template<typename T>
ClassA<T>& ClassA<T>::operator=(const ClassA<T> &rhs)
{ ... }
Run Code Online (Sandbox Code Playgroud)

我假设案例一是正确的.

问题>为什么我们不必ClassA<T>在函数参数列表中使用,例如两个?

c++ templates

5
推荐指数
1
解决办法
117
查看次数

检查迭代器是否引用列表中的项

我只想检查迭代器是否指向列表中的对象.

什么是cmd?

谢谢.:)

SkyThe

编辑:

嗯,好吧,我试过了.现在有一个错误:"表达式:列表迭代器不可复制"

也许一些代码:

#include <list>
list<obj> list;
list<obj>::iterator it;

if(it != list.end()){ //here the error pops up when i debug
  vShowStatus();
}else{
  cout << "...";
}
Run Code Online (Sandbox Code Playgroud)

c++ iterator list

0
推荐指数
1
解决办法
2285
查看次数

用于char*的C中的指针问题

我使用指针来保存名称和研究实验室属性.但是当我打印现有的顶点时,当我打印顶点时,我无法正确看到所谓的属性.例如,虽然名称的实际值是"lancelot",但我认为它是错误的,例如"asdasdasdasd"

struct vertex {
                int value;
                char*name;
                char* researchLab;
                struct vertex *next;
                struct edge *list;
};
    void GRAPHinsertV(Graph G, int value,char*name,char*researchLab) {
    //create new  Vertex.
        Vertex newV = malloc(sizeof newV);
        // set  value of new variable  to which belongs the person.
        newV->value = value;
        newV->name=name;
        newV->researchLab=researchLab;
        newV->next = G->head;
        newV->list = NULL;
        G->head = newV;
        G->V++;
    }

    /***
    The method   creates new person.
    **/
    void createNewPerson(Graph G) {
        int id;
        char name[30];
        char researchLab[30];
        // get requeired …
Run Code Online (Sandbox Code Playgroud)

c printing char

0
推荐指数
1
解决办法
524
查看次数

标签 统计

c++ ×3

c ×1

c++11 ×1

char ×1

iterator ×1

list ×1

printing ×1

templates ×1