我在struct构造函数中检查了一些有关Bitfield的主题,但找不到关于这一行代码的任何解释:
vertex(string s) : name(s) {}在这段代码中:
struct vertex {
typedef pair<int, vertex*> ve;
vector<ve> adj; //cost of edge, destination vertex
string name;
vertex(string s) : name(s) {}
};整个代码结构是关于实现一个加权图的,我在这个网站上看到了:
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;
struct vertex {
typedef pair<int, vertex*> ve;
vector<ve> adj; //cost of edge, destination vertex
string name;
vertex(string s) : name(s) {}
};
class graph
{
public:
typedef map<string, vertex *> vmap;
vmap work;
void addvertex(const string&);
void addedge(const string& from, const string& to, double cost);
};
void graph::addvertex(const string &name)
{
vmap::iterator itr = work.find(name);
if (itr == work.end())
{
vertex *v;
v = new vertex(name);
work[name] = v;
return;
}
cout << "\nVertex already exists!";
}
void graph::addedge(const string& from, const string& to, double cost)
{
vertex *f = (work.find(from)->second);
vertex *t = (work.find(to)->second);
pair<int, vertex *> edge = make_pair(cost, t);
f->adj.push_back(edge);
}这一点是做什么的,目的是什么?
vertex(string s) : name(s) {}发布于 2016-11-17 23:30:46
vertex是结构名,所以vertex(string s)是一个带有string参数的构造函数。在构造函数中,:开始成员初始化列表,该列表初始化成员值并调用成员构造函数。下面的括号是实际的构造函数体,在本例中为空。
有关更多详细信息,请参阅构造函数和成员初始化程序列表。
发布于 2016-11-17 23:28:50
它似乎构造了一个顶点结构。: name(s)语法将struct的name字段设置为s。
https://stackoverflow.com/questions/40666868
复制相似问题