在尝试使用向量实现的列表替换代码并且使用向量工作正常时,我看到错误c2227列表 - > set_as_output'必须指向class / struct / union / generic类型。 我在下面提供了两种实现
class pin {
friend class gate;
pin(){}
~pin() {}
public:
void set_as_input();
void set_as_output();
}; // class pin
class gate {
protected:
std::list<pin *> pins_;
//std::vector<pin *> pins_;
gate();
~gate();
virtual bool validate_structural_semantics();
public:
}; // class gate
class and_gate : public gate
{
bool validate_structural_semantics();
public:
}; // class and_gate
bool and_gate::validate_structural_semantics()
{
if (pins_.size() < 3) return false;
//pins_[0]->set_as_output();//using vectors and works fine
pins_.begin ->set_as_output();//error is here with lists
//for (size_t i = 1; i < pins_.size(); ++i)
for (std::list<pin *>::iterator iter = pins_.begin();
iter != pins_.end(); ++iter)
//pins_[i]->set_as_input();
(*iter)->set_as_input();
return true;
}
我想了解实现中列表和向量之间的区别,以及我的代码中缺少的内容,只要列表涉及到解决问题。 怎么解决?
答案 0 :(得分:0)
列表没有[]运算符,它们不是随机访问容器。
你可以使用auto iter = std :: advance(pins_.begin(),ndx);获取列表的第nx个元素的迭代器。
答案 1 :(得分:0)
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
</style>
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
//create somewhere to put the force directed graph
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var nodes_data = [{
"name": "Travis",
"sex": "M"
},
{
"name": "Rake",
"sex": "M"
},
{
"name": "Diana",
"sex": "F"
},
{
"name": "Rachel",
"sex": "F"
},
{
"name": "Shawn",
"sex": "M"
},
{
"name": "Emerald",
"sex": "F"
}
]
var links_data = [{
"source": "Travis",
"target": "Rake"
},
{
"source": "Diana",
"target": "Rake"
},
{
"source": "Diana",
"target": "Rachel"
},
{
"source": "Rachel",
"target": "Rake"
},
{
"source": "Rachel",
"target": "Shawn"
},
{
"source": "Emerald",
"target": "Rachel"
}
]
//set up the simulation
var simulation = d3.forceSimulation()
//add nodes
.nodes(nodes_data);
//add forces
//we're going to add a charge to each node
//also going to add a centering force
//and a link force
var link_force = d3.forceLink(links_data)
.id(function(d) {
return d.name;
});
simulation
.force("charge_force", d3.forceManyBody())
.force("center_force", d3.forceCenter(width / 2, height / 2))
.force("links", link_force);
//add tick instructions:
simulation.on("tick", tickActions);
//draw circles for the links
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(nodes_data)
.enter()
.append("circle")
.attr("r", 10)
.attr("fill", "red");
//draw lines for the links
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(links_data)
.enter().append("line")
.attr("stroke-width", 2);
var drag_handler = d3.drag()
.on("start", drag_start)
.on("drag", drag_drag)
.on("end", drag_end);
//same as using .call on the node variable as in https://bl.ocks.org/mbostock/4062045
drag_handler(node)
//drag handler
//d is the node
function drag_start(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function drag_drag(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function drag_end(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = d.x;
d.fy = d.y;
}
function tickActions() {
//update circle positions each tick of the simulation
node
.attr("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
});
//update link positions
//simply tells one end of the line to follow one node around
//and the other end of the line to follow the other node around
link
.attr("x1", function(d) {
return d.source.x;
})
.attr("y1", function(d) {
return d.source.y;
})
.attr("x2", function(d) {
return d.target.x;
})
.attr("y2", function(d) {
return d.target.y;
});
}
</script>
pins_.begin ->set_as_output();
是会员功能;你必须打电话给它。正如您已经使用begin
完成的那样,您需要取消引用迭代器来获取指针:
(*iter)->set_as_input()