像往常一样,代码优先:
#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等?
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>
在函数参数列表中使用,例如两个?
我只想检查迭代器是否指向列表中的对象.
什么是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) 我使用指针来保存名称和研究实验室属性.但是当我打印现有的顶点时,当我打印顶点时,我无法正确看到所谓的属性.例如,虽然名称的实际值是"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)