我正在尝试用随机数初始化一个双向量数组(确实是一个矩阵),但我一定有什么问题,而且我无法在这里或在谷歌中找到解决方案。
我有下一个“标准”定义,它用 0 初始化我的矩阵:
情况1:
int num_rows=5, num_cols=7;
std::vector<std::vector<int>> Matrix(num_rows, std::vector<int>(num_cols, 0));
Run Code Online (Sandbox Code Playgroud)
所以如果我做这样的事情:
案例二
std::vector<std::vector<int>> Matrix(num_rows, std::vector<int>(num_cols, rand()%100));
Run Code Online (Sandbox Code Playgroud)
我看到的是它rand()%100被调用一次,所以如果它返回,34那么我的所有矩阵都将填充该数字。
在这一点上,我尝试使用Case 1初始化和 double for 迭代器:
for ( std::vector<std::vector<int>>::iterator it1 = Matrix.begin(); it1 != Matrix.end(); ++it1 )
{
for ( std::vector<int>::iterator it2 = (*it1).begin(); it2 != (*it1).end(); ++ it2 )
{
std::cout << (*it2) << "\n"; //With that I can see every value on the matrix... right now all 0
}
}
Run Code Online (Sandbox Code Playgroud)
现在在那个循环中,我找不到逐项执行并为它们分配新值的方法。我很抱歉,因为我有良心,这是一个非常简单的问题,但在谷歌上找不到......
由于 it2 是我必须使用的 …
我有一个基类和一个子类:
class Base{
public:
Base(){}
// ...
};
class Sub: public Base{
int param;
public:
Sub(){}
// ...
};
Run Code Online (Sandbox Code Playgroud)
我还有一个需要Base向量的函数,如下所示:
void doSomeThing(vector<Base> vectorOfBases){
// ...
}
Run Code Online (Sandbox Code Playgroud)
我需要这样调用该函数:
vector<Sub> myVectorOfSubs;
doSomeThing(myVectorOfSubs);
Run Code Online (Sandbox Code Playgroud)
编译器告诉我:
没有从向量 < Sub > 到向量 < Base > 的适当转换
那么如何将子类向量传递给需要基类向量的函数呢?
我正在std::vectorGDB 中调试 a ,但我无法判断向量认为其当前大小是多少。该向量被声明为std::vector<custom_struct_t *> myVec;。
(gdb) p myVec.size()
Cannot evaluate function -- may be inlined
Run Code Online (Sandbox Code Playgroud)
检查向量:
(gdb) p myVec
[...]
_M_impl = {
[...]
_M_start = 0x8052a0500,
_M_finish = 0x8052a0500,
_M_end_of_storage = 0x8052a0600
}
}, <No data fields>}
Run Code Online (Sandbox Code Playgroud)
在这里我看到_M_start和_M_finish是相同的值。这表示零长度向量还是一长度向量?
我有这个函数可以从这里为我返回 RGB 格式的颜色
std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);
Run Code Online (Sandbox Code Playgroud)
但现在我还希望函数find_dominant_colors生成的图像返回,以便我可以使用它。它生成我使用的三个图像,cv::imwrite但我希望将这三个图像返回给函数调用,以便在返回时可以直接进一步查看它,而不是为它获取目录。
如何在该行代码中解压缩多个值,例如获取图像和颜色,而不仅仅是颜色。我必须使用多个向量吗?我怎么做 ?这里使用的向量是一个 opencv 向量,用于从图像中获取 RGB 值。
编辑 :
std::vector<cv::Vec3b> find_dominant_colors(cv::Mat img, int count) {
const int width = img.cols;
const int height = img.rows;
std::vector<cv::Vec3b> colors = get_dominant_colors(root);
cv::Mat quantized = get_quantized_image(classes, root);
cv::Mat viewable = get_viewable_image(classes);
cv::Mat dom = get_dominant_palette(colors);
cv::imwrite("./classification.png", viewable);
cv::imwrite("./quantized.png", quantized);
cv::imwrite("./palette.png", dom);
return colors;
}
Run Code Online (Sandbox Code Playgroud)
上面的函数将颜色返回到这里
std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);
Run Code Online (Sandbox Code Playgroud)
我也希望它返回viewable quantized dom,我该怎么做?
我进入out_of_range我的代码。我必须如何解决这个问题?有2个功能。第一个函数检查字符串是否为回文。第二个函数必须从向量中找到回文并将其复制到作为返回值的新向量。
#include "pch.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
bool IsPalindrom(string a)
{
string b = a;
reverse(a.begin(), a.end());
if (b == a)
{
cout << "success " << endl;
return true;
}
else {
cout << "error";
return false;
}
}
vector<string> PalindromFilter(vector<string> words, int minLength)
{
vector<string> pol;
for (int i = 0; i <= words.size(); ++i)
{
if (IsPalindrom(words[i]) && words[i].size() > minLength)
{
pol.at(i) = words.at(i);
}
}
return …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <vector>
int main()
{
std::vector<int> sum(1);
sum.push_back(1);
sum[0] = 1;
std::cout << sum[1];
}
Run Code Online (Sandbox Code Playgroud)
为什么结果是1?我从来没有定义 sum[0]。
执行检查时:
std::string st = "hello";
bool is_empty = st.size() > 0;
Run Code Online (Sandbox Code Playgroud)
我收到了上面提到的 Clang-Tidy 警告。
为什么最好使用该empty()方法?
std::distance 在 for 循环中返回相同的值,尽管其中一个参数在每个循环中都发生了变化。
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> q{1, 2, 3, 4, 5};
for (auto i = q.rbegin(); i != q.rend(); i++) {
static int index_dif = distance(i, q.rend());
cout << *i << ": " << index_dif << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是
5: 5
4: 5
3: 5
2: 5
1: 5
Run Code Online (Sandbox Code Playgroud)
尽管i每个循环都会增加,所以我希望q.rend()它之间的距离随着循环的进行而缩小,如下所示:
5: 5
4: 4
3: 3
2: 2
1: 1
Run Code Online (Sandbox Code Playgroud)
相反,它似乎给人之间的距离q.rbegin()和q.rend() …
我在std::vector课堂上遇到了问题,我创建了一个struct
struct Triplet{
int first;
int second;
int third;
};
Run Code Online (Sandbox Code Playgroud)
我创建了一个vector<Triplet> T. 我的问题是它不会包含我需要的元素,即使T.max_size() = 357913941我只得到 T.size() = 60540697或 T.size() = 40360465使用该函数
vector<Triplet> T;
while(true)
{
Triplet t;
t.first = 1; t.second = 1 ; t.third = 1;
try {
T.push_back(t);
} catch (...) {
break;
}
}
qDebug() << T.size();
Run Code Online (Sandbox Code Playgroud)
谁能解释一下为什么要这样做?我在 Windows 10 和 16Go 的 RAM 上运行,使用 Qt 和 VSC++ 2017 x86(由于我无法为 x64 编译的 Lemon 库),
我有以下代码 c++ 代码
#include <vector>
#include <iostream>
using namespace std;
#define for_loop(upper_bound) for (int i=0;i<upper_bound; ++i)
// #define SHOW_VECTOR(vec_in) for(int j=0;j<vec_in.size();j++){ cout << vec_in[j] << " " << endl;};
int main(){
int dim_1=10;
int dim_2=3;
int outer_i;
// vector variable declared here to have 10 values
vector<vector<int>>vec_var(dim_1);
for_loop(dim_1){
outer_i = i;
for_loop(dim_2){
cout << outer_i << " " << i << endl;
vec_var[outer_i][i]=103;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,出现以下错误:
分段错误(核心转储)
c++ ×10
stdvector ×10
vector ×4
containers ×2
std ×2
clang-tidy ×1
distance ×1
for-loop ×1
gdb ×1
iterator ×1
opencv ×1
palindrome ×1
pointers ×1
reverse ×1