大家好,我有这个构造函数
Matrix::Matrix(size_t row, size_t col)
{
if(row < 1 || col < 1)
throw new std::runtime_error("Minimalni velikost matice je 1x1");
matrix = std::vector<std::vector< double > >(row,std::vector<double>(col, 0));
}
Run Code Online (Sandbox Code Playgroud)
和这个测试
Matrix *TestedMatrix;
EXPECT_THROW(TestedMatrix = new Matrix(-2,3),std::runtime_error );
Run Code Online (Sandbox Code Playgroud)
但我仍然得到那个例外是不同类型的。我也试过,std::runtime_error*但结果是一样的。一开始我想使用 EXPECT_ANY_THROW 但它没有显示在我的代码覆盖率中。感谢帮助 !:)
我正在学习c ++和测试问题我正在使用带有条件的for循环,prices.size()-1因此向量不会超出范围.
std::vector<int> prices {7,1,5,3,6,4};
int maxProfit(vector<int>& prices) {
int total {0};
for (size_t i = 0; i < prices.size()-1; i++) {
if (prices.at(i+1) > prices.at(i)) {
total += prices.at(i+1) - prices.at(i);
}
}
return total;
}
Run Code Online (Sandbox Code Playgroud)
但它抛出了这个我无法破译的运行时错误:
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 1) >= this->size() (which is 0)
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚问题,有人可以告诉我我做错了什么.
在多线程程序中调用 fork() 会阻塞所有线程吗?或者它只会阻塞调用线程?
('调用fork()'是指创建子进程的过程。)
int main()
{
vector<int> v={1,2,4};
int &a=*v.begin();
cout<<a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
v.begin()在上面的代码段中,分配给 的迭代器的取消引用值&a。打印a显示 1
int main()
{
vector<int> v={1,2,4};
int a=*v.begin();
cout<<a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这里,迭代器被引用,并且值被分配给a(不带 &)。打印a仍然显示 1。
我仍在学习 C++,我不明白为什么第一个代码块有效。意味着什么int &a?
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
vector<vector<int> >a(2);
for(int i=0;i<2;i++)
{
int m;
cout<<"Enter number of elements on the row:"<<endl;
cin>>m;
vector<int> b(m);
cout<<"Element:"<<endl;
for(int j=0;j<m;j++)
{
int k;
cin>>k;
b.push_back(k)//if i use b[j]=k; here the zeros don't appear.
//cout<<b[j];
}
a.push_back(b);
}
for(unsigned int i=0;i<a.size();i++)
{
for(unsigned int j=0;j<a[i].size();j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么输出中出现零?
输出应该不带零。
在第二个push_back函数中,如果我使用b[j]则不会出现零。
我正在尝试学习 C++,当我尝试运行我正在创建的这个简单程序时,出现以下错误。
Error C3867 'budget::check_mort': non-standard syntax; use '&' to create a pointer to member
Error C3867 'budget::check_car': non-standard syntax; use '&' to create a pointer to member
Error C3867 'budget::check_groc': non-standard syntax; use '&' to create a pointer to member
Error C3867 'budget::check_util': non-standard syntax; use '&' to create a pointer to member
Run Code Online (Sandbox Code Playgroud)
我不太确定从这里该去哪里。搜索问题似乎表明需要将某些东西设为静态或常量,但我不太确定。任何帮助将非常感激。
使用 Visual Studio 社区和 Windows 10 文件如下:
.h 文件
#pragma once
#include <iostream>
#include <string>
#include <ostream>
#include <list>
#include <algorithm>
#include <numeric>
#include <windows.h> …Run Code Online (Sandbox Code Playgroud) 因此,我正在制作一款数独游戏,其中每个方格都会指向其行、列和 3x3“区域”中的所有其他方格。在sudoku构造函数中,我连接了相关的方块。行和列连接不会给我任何意外的结果 \xe2\x80\x93 有 1296 个连接需要建立,当该部分完成时,打印显示count正好 1296。太棒了!
现在,当我对 3x3 区域执行相同操作时,我遇到了问题。当使用 时option 2,count结束于 324,当打印板时我得到了想要的结果。然而,当将函数的结果存储get_node在临时变量 ( option1) 中时,我得到 648,并且每个方块的连接计数是正确的(16 而不是 20)。情况不应该是这样,因为该get_node函数所做的只是return _board[(I * dim) + J]. 有谁知道这样的问题可能来自哪里?
提前致谢!
\nPS 是的,我暂时禁用了编译器警告,因此goto语句将编译 \xe2\x80\x93 这不是问题,这只是为了让这里的人们更清楚地看到:)
// areas\nint count(0);\nfor (int a = 0; a < 3; ++a) {\n for (int b = 0; b < 3; ++b) {\n for (int i = 0; i < …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
std::string stream;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, & stream);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](void *buffer, size_t size, size_t count, std::string *stream) -> size_t {
auto s_size = size * count;
stream->append((char *)buffer, 0, s_size);
return s_size;
});
Run Code Online (Sandbox Code Playgroud)
它编译并崩溃。如果我用函数替换 lambda,它就可以正常工作:
static size_t _writefunction(void *buffer, size_t size, size_t count, std::string *stream) {
auto s_size = size * count;
stream->append((char *)buffer, 0, s_size);
return s_size;
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以使用 lambda 代替分离的函数吗?
在 C++ 中,请解释string = ""向量声明中的含义->vector<string = "">v;
我收到这个错误main.cpp:23:5: error: \xe2\x80\x98Department\xe2\x80\x99 does not name a type
/* \n * File: main.cpp\n * Author: anonymous\n *\n * Created on May 11, 2015, 9:44 PM\n */\n\n#include <cstdlib>\n#include <iostream>\n\nusing namespace std;\n\nclass College{\n public :\n string name; //for name of the college\n int numOfColleges;\n int numDepartments; //number of departments in this college\n Department* dep; //this will point to the department in this college\n College* next; //the next pointer to point to the next college\n College(){\n name =" "; \n numDepartments=0 …Run Code Online (Sandbox Code Playgroud)