我写了一个小程序:
void showrecord()
{
char *a[]={ "O_BILLABLE_ACCOUNT","O_CUSTOMER_TYPE_INDICATOR",
"O_A_PARTY_MSISDN_ID","O_A_PARTY_EQUIPMENT_NUMBER",
"O_A_PARTY_IMSI","O_A_PARTY_LOCATION_INFO_CELL_ID",
...
};
vector<std::string> fields(a,a+75);
cout<<"done!!!"<<endl;
}
int main()
{
showrecord();
}
Run Code Online (Sandbox Code Playgroud)
我有一些字符串文字,我希望它们被复制到一个向量中.我没有找到任何其他简单的方法:(.或者如果有任何直接的方法来初始化矢量而不使用数组,那将非常有用.这是在我在unix上运行可执行文件后转储核心.它给了我一个警告,但是:
Warning 829: "test.cpp", line 12
# Implicit conversion of string literal to 'char *' is deprecated.
D_TYPE","O_VARCHAR_5","O_VARCHAR_6","O_VARCHAR_7","O_VARCHAR_8","O_VARCHAR_9"};
Run Code Online (Sandbox Code Playgroud)
但是相同的代码在Windows上正常运行没有任何问题.我在HPUX上使用编译器aCC.
请帮忙! 下面的编辑是转储的堆栈跟踪.
(gdb) where
#0 0x6800ad94 in strlen+0xc () from /usr/lib/libc.2
#1 0xabc0 in std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string<char,std::char_traits<char>,std::allocator<char>>+0x20 ()
#2 0xae9c in std<char const **,std::basic_string<char,std::char_traits<char>,std::allocator<char>> *,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>::uninitialized_copy+0x60 ()
#3 0x9ccc in _C_init_aux__Q2_3std6vectorXTQ2_3std12basic_stringXTcTQ2_3std11char_traitsXTc_TQ2_3std9allocatorXTc__TQ2_3std9allocatorXTQ2_3std12basic_stringXTcTQ2_3std11char_traitsXTc_TQ2_3std9allocatorXTc____XTPPCc_FPPCcT118_RW_is_not_integer+0x2d8
()
#4 0x9624 in showrecord () at test.cpp:13
#5 0xdbd8 in …Run Code Online (Sandbox Code Playgroud) 通过阅读std::vector我明白的参考
insert当达到最大容量时调用将导致重新分配std::vector(导致迭代器失效),因为为其分配了更大容量的新内存.目标是保证连续数据的保证.
只要我坚持低于最大容量insert就不会导致(并且迭代器将完好无损).
我的问题如下:
当reserve自动调用时insert,有没有办法控制必须保留多少新内存?
假设我有一个初始容量为100的向量,当达到最大容量时,我想分配额外的20个字节.
有可能吗?
学科.
编译:g ++ -g -Wall -o program program.cpp
正在运行:gdb -tui程序
谢谢,
哈维尔.
这可能是一个新问题(我),但我已尽可能多地搜索以找到解决以下问题的方法
我有以下场景(当然很重要):
class Container
{
std::vector<Object> obj;
};
class Pointers
{
std::vector<Object*> obj_ptr;
};
Run Code Online (Sandbox Code Playgroud)
我有一个例程,它将Object类型的元素推回到Container中的向量obj,然后将指向同一元素的指针推回到obj_ptr.
整个想法是obj_ptr [i] == &obj [i]在整个程序的整个生命周期.
我遇到的问题是,每当obj的容量需要增加所有指针都无效时,使obj_ptr完全无用.我使用最大预期大小(大约10 ^ 7)尝试了两个obj.reserve()并初始化具有相同大小的向量.问题仍然存在.
不确定它是否重要,但我正在使用VS 2015 Com.
谢谢!
由于找不到比较运算符,因此以下代码无法编译。
#include <vector>
#include <iostream>
#include <string>
namespace Cool {
struct Person {
std::string name;
};
}
bool operator==(const Cool::Person& p1, const Cool::Person& p2) {
return p1.name == p2.name;
}
int main(int, char *[])
{
std::vector<Cool::Person> a{ {"test"} };
std::vector<Cool::Person> b{ {"test"} };
bool ok = a == b;
std::cout << ok << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
经过一些实验,我发现以下代码可以完美编译:
#include <vector>
#include <iostream>
#include <string>
namespace Cool {
struct Person {
std::string name;
};
bool operator==(const Person& p1, const Person& p2) {
return …Run Code Online (Sandbox Code Playgroud) 如何将可变参数构造函数参数存储到向量?
我失败的尝试示例:
class Combo
{
public:
template <class... Args>
Combo(Args... args)
{
// this->keys_.push_back(args...);
// this->keys_.push_back(args)...;
// this->keys_.push_back(std::forward<Args>(args...));
//for (uint8_t arg : args...)
// this->keys_.push_back(arg);
// ???
}
private:
std::vector<uint8_t> keys_;
};
Run Code Online (Sandbox Code Playgroud) 我正在学习 CPP++14 移动语义。在编写一小段代码时,我观察到了一些奇怪的行为。我正在使用 r 值引用将唯一 ptr 的向量移动到函数。在调试时,我发现更改也应用于移动的对象。为什么即使物体被移动我也会观察这个hcnage?以下代码中的移动有什么作用?
void func(std::vector<std::unique_ptr<int>> && vect) {
vect.emplace_back(std::move(std::make_unique<int>(3)));
return ;
}
int main() {
std::vector<std::unique_ptr<int>> a;
func(std::move(a));
cout<<(*(a[0]))<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个类型X,它是可复制构造但不可复制分配的
struct X {
X();
X(const X&);
X& operator=(const X&) = delete; // !!
X(X&&) noexcept;
X& operator=(X&&) noexcept;
int data = 54;
};
Run Code Online (Sandbox Code Playgroud)
我有“X”两个向量:a和b我想插入的所有内容b在前面a:
void add_to_front(std::vector<X>& a, const std::vector<X>& b) {
a.insert(a.begin(), b.begin(), b.end());
}
Run Code Online (Sandbox Code Playgroud)
这在 msvc 上编译并按预期工作,但无法在 clang 和 gcc 上编译。我猜是由于 libc++ 和 libstdc++ 的糟糕实现,即使它永远不会被调用(或者,更糟糕的是,它会被调用!?),它们需要一些东西来编译。
我可以写一本手册回路emplace的元素b进入a,这将产生一个正确的结果,但这样的复杂性是A * B,而不是A + B,因为每次调用emplace将转移的所有元素a一遍又一遍。
那么有没有有效的方法呢?
我对 C++ 比较陌生,我尝试做一些研究,但是在网上搜索时,我主要遇到的是 C 数组而不是std::array. 将 std::array 元素附加到 std::vector 并将std::array元素插入到 a中的最有效方法是std::vector什么?我应该使用 STL 函数,例如std::copy? 我目前正在使用 C++17,MinGW64。
我不知道,为什么它输出1024?
vector<int> default_container = { 1,2,3,4,5,6,7,78,8,1024 };
cout << *default_container.end() << endl; // 0
default_container.pop_back();
for (auto it : default_container)
{
cout << it << ",";
}
cout << endl;
cout << *default_container.end() << endl; // 1024 why?why?why?why?
cout << *--default_container.end() << endl; // 8
Run Code Online (Sandbox Code Playgroud) c++ ×10
stdvector ×10
stl ×2
c++11 ×1
c++17 ×1
constructor ×1
dereference ×1
gdb ×1
insert ×1
iterator ×1
namespaces ×1
pointers ×1
stdarray ×1
stdmove ×1
templates ×1
unique-ptr ×1
vector ×1