本质上,我希望我的两个类共享一些数据,我希望将这些数据作为shared_ptr对象的向量。
我已将代码缩减为以下简单的可编译示例。
我希望对象 A 查看对象 B 中初始化的数据。但是,当我尝试在 A 的方法内执行 Push_back() 时,它并没有更改 B 中的共享指针对象的向量的大小。(在与评论“这是兴趣点”一致。)
我需要使用什么方法来获得此功能,或者我走错了路。(这里是c++新手)
#include <memory>
#include <vector>
#include <iostream>
using std::cout;
using std::endl;
class DataClass {
public:
int i_;
};
class B {
public:
// constructor:
B() : my_data_(std::vector<std::shared_ptr<DataClass> >()) {
my_data_.push_back(std::shared_ptr<DataClass> (new DataClass));
my_data_.push_back(std::shared_ptr<DataClass> (new DataClass));
my_data_[0]->i_ = 1;
my_data_[1]->i_ = 2;
cout<<my_data_.size()<<endl;
};
// return the data
std::vector< std::shared_ptr<DataClass> > get_my_data() {
return my_data_;
};
// check the data:
void CheckData() {
cout<<my_data_.size()<<endl; // This is the …Run Code Online (Sandbox Code Playgroud) 例如,我在模板类中有这样的结构:
struct Foo{
int data;
vector<Foo*> children;
}
Run Code Online (Sandbox Code Playgroud)
要打印出数据值,我可以简单地执行以下操作:(设为bar指向 a 的指针Foo)
print bar->data
Run Code Online (Sandbox Code Playgroud)
这很好用。不过我还想关注children另一个Foo。我试过:
print bar->children[0]->data
Run Code Online (Sandbox Code Playgroud)
但它不起作用。我应该如何访问向量中的项目并在中使用它print?
是否可以将 NSMutableArray 的内容转换为 std::vector ?如果是这样,应该在 Objective-C 或 C++ 代码中完成吗?
我有以下代码来分配大量数据,如果它超过可用内存(这里是 32GB),它应该抛出异常。使用:
bool MyObject::init()
{
char* emergency_memory = new char[32768];
try
{
std::vector<std::vector<MyData> > data_list;
std::vector<MyData> data;
data.resize(1000);
for(size_t i=0; i<1000; i++)
{
data_list.push_back(data);
data_list.push_back(data);
}
}
catch (const std::bad_alloc& e)
{
delete[] emergency_memory;
std::cout << "Data Allocation failed:" << e.what() << std::endl;
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
异常永远不会被捕获。应用程序刚刚终止,或者使操作系统崩溃。
我做错了什么?
我知道以下情况的区别:
case 1: int a[10];
Run Code Online (Sandbox Code Playgroud)
对于情况 1,数组的内存在堆栈上分配。
case 2: int *a = new int[10];
Run Code Online (Sandbox Code Playgroud)
对于情况 2,在堆上分配内存并返回一个指针。
但是下面两个声明有什么区别,因为向量内存总是分配在堆上
vector<int> v1;
vector<int> *v2 = new vector<int>();
Run Code Online (Sandbox Code Playgroud) c++ stdvector dynamic-memory-allocation static-memory-allocation
vectors[0]为什么我已经pop_back()迈出了第一步仍然可以获得价值?
我猜现在我的vectors已经空了,请致电检查empty()
#include <iostream>
#include <vector>
struct Vector2 {
int x, y;
Vector2(int _x, int _y)
: x(_x)
, y(_y) {}
Vector2(const Vector2& vec)
: x(vec.x)
, y(vec.y) {
std::cout << "[Copy] Vector2{" << x << "," << y << "} copied!"
<< std::endl;
}
void Print() {
std::cout << "[Print] Vector2{" << x << "," << y << "}" << std::endl;
}
};
void PrintVectors(const std::vector<Vector2>& vectors) {
std::cout << "---" << std::endl; …Run Code Online (Sandbox Code Playgroud) 假设我有一个std::vector<T> fromandstd::vector<T> to其中T是不可复制但可移动的类型,并且to可能为空,也可能不为空。我希望所有元素都from附加在 后面to。
如果我使用std::vector<T>::insert(const_iterator pos, InputIt first, InputIt last)重载 (4) with pos = to.end(),它将尝试复制所有对象。
现在,如果to.empty()我可以的话std::move(from),否则我可以先手动from.reserve(from.size()+to.size())然后手动to.emplace_back(std::move(from[i]))每个元素from,最后from.clear()。
有没有直接的方法可以std使用方便的函数或包装器来做到这一点?
如果我想编辑向量的每个元素,我可以使用for_each()循环遍历元素。现在的问题是,如何将这个任务分成两个线程?
我已经尝试了下面的方法,用 声明了一个线程for_each(),但是我遇到了错误。
例如,我想为向量的每个元素加 1。通过使用线程,似乎我错过了编译器不喜欢的东西。
#include <iostream>
#include <algorithm>
#include <vector>
#include <thread>
using namespace std;
int main()
{
std::vector<int> nums; //declare a vector
nums.push_back(1);
nums.push_back(2);
nums.push_back(3);
nums.push_back(4); //push each element to the vector
size_t i = (nums.size()/2); //I want to separate the task into two thread
std::thread t1(std::for_each(nums.begin(),nums.begin()+i,[](int& num){
num++;
}));
std::thread t2(std::for_each(nums.begin()+i,nums.end(),[](int& num){
num++;
}));
t1.join();
t2.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到这两个错误:
未能专门化函数模板 'unknown-type std::invoke(_Callable &&) noexcept(<expr>)'
和
invoke': 找不到匹配的重载函数
如果我不能以这种方式做线程,那么正确的方法是什么?
我正在尝试使用其insert(const_iterator pos, InputIt first, InputIt last)成员函数模板将转换后的目录条目范围插入向量中。不幸的是,我无法在 GCC 下编译以下代码,11.1.0根据https://en.cppreference.com/w/cpp/compiler_support应该有范围支持。
#include <filesystem>
#include <vector>
#include <ranges>
#include <iterator>
namespace fs = std::filesystem;
namespace ranges = std::ranges;
namespace views = std::views;
// no solution
namespace std {
template <typename F>
struct iterator_traits<ranges::transform_view<ranges::ref_view<fs::directory_iterator>, F>> {
using iterator_category = std::input_iterator_tag;
};
}
int main() {
std::vector<fs::path> directory_tree;
auto subdir = fs::directory_iterator(".");
ranges::input_range auto subdir_names = subdir
| views::transform([](const auto& entry) { return entry.path(); /* can be more complex*/ }) …Run Code Online (Sandbox Code Playgroud) C++03 标准是否允许将 a 附加std::vector到自身?v我想知道如果需要重新分配内存,源迭代器是否会变得无效。在我的 STL 实现中,旧内存会一直保留,直到创建新内存为止。但我可以依靠这个吗?如果不是,v.reserve(2 * v.size())在插入之前是否是完全避免重新分配的良好解决方案?
vector<int> v;
v.reserve(3);
v.push_back(1);
v.push_back(2);
v.push_back(3);
// v may need to reallocate because its capacity may be less than 6.
// Is this operation safe?
v.insert(v.end(), v.cbegin(), v.cend());
Run Code Online (Sandbox Code Playgroud)
或者
// Here v will _not_ need to reallocate because it has enough capacity.
// Is this operation safe?
v.reserve(2 * v.size());
v.insert(v.end(), v.cbegin(), v.cend());
Run Code Online (Sandbox Code Playgroud) c++ ×10
stdvector ×10
stl ×2
algorithm ×1
bad-alloc ×1
c++03 ×1
c++17 ×1
c++20 ×1
gdb ×1
ios ×1
objective-c ×1
shared-ptr ×1
std-ranges ×1
vector ×1