是否std::vector::pop_back设置它的对象的指针,nullptr或者它只是删除的对象?
我看到我的向量的大小减小了,所以对象显然被删除了,但我想知道指针是设置为nullptr还是必须手动执行?
编辑:我根据包含指针的向量提出了这个问题。例子:vector<Bitmap*>。
我对 C++ 比较陌生,对一些奇怪的行为感到困惑。我得到一个包含 std::vector 的对象。然后,我用完全相同的复制行打印出它的大小两次:
Pose& pose = getCurrentPose();
std::cout << "nr1: " << pose.transforms.size() << " bones." << std::endl;
std::cout << "nr2: " << pose.transforms.size() << " bones." << std::endl;
Run Code Online (Sandbox Code Playgroud)
结果:
Nr1: 17 bones.
Nr2: 38294074 bones.
Run Code Online (Sandbox Code Playgroud)
对此向量大小的任何进一步调用都会返回相同的巨大数字(17 应该是正确的)。
迭代向量时我也会出错。在我看来,它实际上并未调整大小,但某种端点已损坏。这里发生了什么,我该如何解决这个问题?
getCurrentPose 大致如下所示:
Pose& getCurrentPose() {
Pose& accu;
for (int b = 0; b < p.transforms.size(); b++) {
BoneState bs;
accu->transforms.push_back(bs);
}
for (int b = 0; b < p.transforms.size(); b++) {
accu->transforms.at(b).loc += getLoc(b);
accu->transforms.at(b).rot += getRot(b);
accu->transforms.at(b).scale += …Run Code Online (Sandbox Code Playgroud) 我正在学习c++,我想在c++中实现以下python代码:
C = np.where(A>B)[0]
while len(C)>0:
d = C[0]
# do something to A[d] and B[d]
C = A>B
Run Code Online (Sandbox Code Playgroud)
A和B都是相同长度的向量。在 C++ 中,我知道如何声明、初始化A和B使用vector,并为 A 和 B 实现中间的“做某事部分”,但我不知道如何比较它们并检查是否A有大于 的元素B,并找到索引发生这种情况的元素。
我在这里看过很多文章,但在比较的情况下,仍然没有弄清楚函子类相对于简单函数的优势。所以我有一个代码摘录,他们想unsigned char *通过简单地遍历每个字符并从左到右比较具有优先级的 ASCII 代码来对向量进行排序。
函子是
class Sorter {
private:
unsigned int length;
public:
Sorter( unsigned int _length ): length( _length ) {}
bool operator()( const unsigned char* keyl, const unsigned char* keyr ) { return cmpKey( keyl, keyr, length ); }
};
Run Code Online (Sandbox Code Playgroud)
并且该cmpKey()功能基本上执行我上面描述的操作。对数据进行排序的调用是
sort( localList.begin(), localList.end(), Sorter( 100 ) );
Run Code Online (Sandbox Code Playgroud)
其中 100 是每个字符串的长度。所以,我读过函子的优点是可以存储调用到调用的阶段,并允许将类用作普通函数。我有两个问题:
在这种情况下,这样做有什么好处?
Sorter将创建多少个类实例?它是只有 1 还是与 的元素一样多locaList?
我需要使用 OpenGL 在 xy 平面中生成一个 NxN 分辨率的网格。最终的网格需要由三角形 ( GL_TRIANGLES) 组成,因此它应该类似于以下示例:
^
* | ----------
* | |\ |\ |\ |
* | | \| \| \|
* | ----------
* | |\ |\ |\ |
* y | | \| \| \|
* | ----------
* | |\ |\ |\ |
* | | \| \| \|
* | ----------
* |
* |-------------->
* x
Run Code Online (Sandbox Code Playgroud)
请注意,我需要以顶点索引形式存储网格(通过参数给定结构)。
我此时的代码:
void generate_grid(
std::uint32_t N,
std::vector<glm::vec3>* vertices,
std::vector<glm::uvec3>* indices)
{
for (int …Run Code Online (Sandbox Code Playgroud) 我试图学习C ++中的存储桶排序算法,发现以下代码:
// C++ program to sort an array using bucket sort
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// Function to sort arr[] of size n using bucket sort
void bucketSort(float arr[], int n)
{
// 1) Create n empty buckets
vector<float> b[n];
// 2) Put array elements in different buckets
for (int i = 0; i < n; i++)
{
int bi = n * arr[i]; // Index in bucket
b[bi].push_back(arr[i]);
}
// 3) Sort individual …Run Code Online (Sandbox Code Playgroud) 我具有以下向量值:[2, 3, 7]。
我想输出向量的乘积,如中所示2*3*7 = 42。
我为此写了一些代码,但是似乎没有用。我是C ++的新手,因此在给定任何大小的数值矢量的情况下,我不确定如何获取矢量值的乘积。
#include <bits/stdc++.h>
int main()
{
int n;
cin >> n;
vector<int> vec;
while (n--)
{
int temp;
cin >> temp;
vec.push_back(temp);
}
int total = 1;
total *= vec;
cout << vec << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在执行一些关于移动语义的测试,我的类行为对我来说似乎很奇怪。
鉴于模拟类VecOfInt:
class VecOfInt {
public:
VecOfInt(size_t num) : m_size(num), m_data(new int[m_size]) {}
~VecOfInt() { delete[] m_data; }
VecOfInt(VecOfInt const& other) : m_size(other.m_size), m_data(new int[m_size]) {
std::cout << "copy..." <<std::endl;
std::copy(other.m_data, other.m_data + m_size, m_data);
}
VecOfInt(VecOfInt&& other) : m_size(other.m_size) {
std::cout << "move..." << std::endl;
m_data = other.m_data;
other.m_data = nullptr;
}
VecOfInt& operator=(VecOfInt const& other) {
std::cout << "copy assignment..." << std::endl;
m_size = other.m_size;
delete m_data;
m_data = nullptr;
m_data = new int[m_size];
m_data …Run Code Online (Sandbox Code Playgroud) 我正在实现的算法具有以下结构:
while C is not empty
select a random entry e from C
if some condition on e
append some new entries to C (I don't care where)
else
remove e from C
Run Code Online (Sandbox Code Playgroud)
重要的是循环 e 的每次迭代都是随机选择的(具有统一的概率)。
理想情况下select,append和remove步骤都是 O(1)。
如果正确地明白,使用std::list的append和remove步骤将是O(1),但随机选择将是O(N)(例如,使用std::advance如在此解决方案)。
And std::deque and std::vector seem to have complementary O(1) and O(n) operations.
I'm guessing that std::set will introduce some O(log n) complexity.
Is there any …
我需要在不同的线程上填充一个 std::vector。
它是正确的代码吗?或者我应该为我的代码添加互斥锁?
void func(int i, std::vector<float>& vec)
{
vec[i] = i;
}
int main()
{
std::vector<float> vec(6);
std::list<std::thread> threads;
for (int i = 0; i < 6; i++)
{
threads.push_back(std::thread(func, i, std::ref(vec)));
}
for (auto iter = threads.begin(); iter != threads.end(); iter++)
{
(*iter).join();
}
}
Run Code Online (Sandbox Code Playgroud)
我测试了我的代码它工作正常。有什么陷阱吗?它是线程安全的代码吗?
如何通过不同的线程获取 std::vector 数据?