我将vector定义为Grid类的私有变量.Class Points只有两个实例变量,它们都是整数,但只有当我从文件中读取它时,才会知道点的数量,所以我想我必须用new动态创建Points,这意味着我必须在以后销毁它们.我是否正确地初始化了构造函数?当为Grid编写析构函数时,我需要为这样的向量编写析构函数:~vecotr()或删除或使用迭代器?
class Grid{
public:
// initialize vector with 3 points with val 0.
Grid(vector<Points> v) : vector<Points>(3, 0) {}; // is this right
// first option
~Grid() {
~vector<Points>(); // not sure how to destroy vector<Points>;
}
// second option
~Grid() {
delete v_points;
}
// third option
~Grid() {
for (vector<Points>::iterator it = v_points.begin(),
vector<Points>::iterator it_end = v_points.end(); it != it_end; it++)
}
private:
vector<Points> v_points;
};
Run Code Online (Sandbox Code Playgroud)
我应该使用哪个选项并正确初始化构造函数?
我想返回一个Vertex &,这是代码:
Vertex& Graph::getVertex(std::string v) { // gets the vertex
for (std::vector<Vertex>::iterator it = vertices.begin(); it != vertices.end(); it++) {
if ((it->getName()).compare(v) == 0)
return it; // if strings are the same return vertex
}
exit(1);
}
Run Code Online (Sandbox Code Playgroud)
问题是getVertex标记为不兼容并且it在返回标记为类型的引用Vertex &(非const限定)不能使用类型的值初始化std::vector...我将如何修复这些错误?
在阅读了计算机程序的结构和解释(SICP)之后,我决定找到一种方法来使用C实现其中一些函数式编程技术.我试着编写一个程序,它创建了一对,其第一个参数是函数的名称,第二个是arg是任何带有一个arg并返回一个arg的函数.使用下面的实现我期望看到如下输出:
fact(7) = 5040
fib(7) = 13
Run Code Online (Sandbox Code Playgroud)
但相反,我得到了
fact(7) = 5040
fib(7) = 0
Run Code Online (Sandbox Code Playgroud)
以及警告
$ cc map.c
map.c: In function ‘main’:
map.c:41:17: warning: assignment from incompatible pointer type [enabled by default]
maps[0].f_ptr = &fact;
^
map.c:43:17: warning: assignment from incompatible pointer type [enabled by default]
maps[1].f_ptr = &fib;
^
map.c:47:7: warning: passing argument 1 of ‘maps[i].f_ptr’ makes pointer from integer without a cast [enabled by default]
ans = (int) maps[i].f_ptr((int) num);
^
map.c:47:7: note: expected ‘void *’ but argument …Run Code Online (Sandbox Code Playgroud) 我有一个矢量:
std::vector<Edge> edges(num);
Run Code Online (Sandbox Code Playgroud)
最后包含空单元格,我想用lambda删除但是当我这样做时:
edges.erase(std::remove_if(edges.begin(), edges.end(),
std::mem_fn(&std::Edge::empty), edges.end() );
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
error C2664: 'bool main::<lambda_6f33349b59d49f69703a5fa6a8c5995a>::operator ()(const std::vector<_Ty> &) const' : cannot convert parameter 1 from 'Edge' to 'const std::vector<_Ty> &'
Run Code Online (Sandbox Code Playgroud)
现在我该怎么做?
我输入来自文件input.txt作为两列字符串,例如:
string1 string2
string3 string4
etc.
Run Code Online (Sandbox Code Playgroud)
我试图从0开始按升序对字符串进行编号,但这样的方式是重复字符串不会被分配新的值,但保持一次已分配给它们.我决定使用set :: find操作来完成这项工作,但我很难让它工作.这是我到目前为止所拥有的:
int main(int argc, char* argv[]) {
std::ifstream myfile ("input.txt");
std::string line;
int num = 0; // num is the total number of input strings
if (myfile.is_open()) {
while(std::getline(myfile, line)) {
++num;
}
}
std::string str1, str1; // strings form input
int str1Num, str2Num; // numbers assigned to strings
int i = 0; // used to assign values to strings
StringInt si;
std::vector<StringInt> saveStringInts(num);
std::set<std::string> alreadyCounted(num, 0);
std::set<std::string>::iterator sit;
std::ifstream myfile2 ("input.txt"); …Run Code Online (Sandbox Code Playgroud)