以下函数应该计算a的中位数std::vector<double>.
double vecMed(vector<double>& vec) {
vector<double> copyVec = vec;
sort(copyVec.begin(), copyVec.end());
if( copyVec.size()%2 == 0)
return (copyVec[floor(static_cast<double>(copyVec.size())/2.0)] + copyVec[ceil(static_cast<double>(copyVec.size())/2.0)]) / 2.0;
else
return copyVec[copyVec.size()/2];
}
Run Code Online (Sandbox Code Playgroud)
我在这条线上遇到了一个段错误,有时候:
return (copyVec[floor(static_cast<double>(copyVec.size())/2.0)] + copyVec[ceil(static_cast<double>(copyVec.size())/2.0)]) / 2.0;
Run Code Online (Sandbox Code Playgroud)
任何人都可以看到这个功能的问题?
段错误可能是由于我不知道的其他地方的内存损坏,但我想确保在我探索这种可能性之前,我没有在上面的代码中做出一个微妙或粗心的错误.
GDB说:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000428ab6 in vecMed (this=0x7fffffffd1b0, vec=...) at ../globals.cpp:834
834 return (copyVec[floor(static_cast<double>(copyVec.size())/2.0)] + copyVec[ceil(static_cast<double>(copyVec.size())/2.0)]) / 2.0;
(gdb) bt
#0 0x0000000000428ab6 in vecMed (this=0x7fffffffd1b0, vec=...) at ../globals.cpp:834
Run Code Online (Sandbox Code Playgroud) 我一直在寻找这个错误的解决方案一段时间没有成功.这个话题有几个答案,但根本没有帮助我.我正在使用代码块12.11编译Linux Mint,这是一个简单的C++程序,我遇到过
error: 'array' is not a member of 'std'
其他一些答案建议检查编译器设置:设置 - >编译器 - >编译器设置 - >编译器标志,我想的是:让g ++遵循C++ 11 ISO C++语言标准[-std = c ++ 11 ].我仍然有同样的错误.有人可以帮忙吗?
我想将 Crypto++ 密钥保存到std::vector<uint8_t>. 不幸的是,只有CryptoPP::StringSink, that 需要std::string参考,但没有CryptoPP::VectorSink那个会参考std::vector.
以下代码工作正常
std::string spki;
CryptoPP::StringSink ss(spki);
CryptoPP::RSA::PublicKey publicKey(...);
publicKey.Save(ss);
Run Code Online (Sandbox Code Playgroud)
但我想要这个
std::vector<uint8_t> spki;
CryptoPP::VectorSink vs(spki);
CryptoPP::RSA::PublicKey publicKey(...);
publicKey.Save(vs);
Run Code Online (Sandbox Code Playgroud)
问题
VectorSink不能仅通过使用 typedef 来创建,因为 traits_type::char_typeinside StringSinkTemplate:
using CryptoPP::StringSinkTemplate;
typedef StringSinkTemplate< std::vector<byte> > VectorSink;
In file included from cryptopp-test.cpp:65:
In file included from /usr/local/include/cryptopp/files.h:5:
/usr/local/include/cryptopp/filters.h:590:22: error: no member named
'traits_type' in 'std::vector<unsigned char, std::allocator<unsigned char>
>'
typedef typename T::traits_type::char_type char_type;
~~~^
cryptopp-test.cpp:243:20: note: in instantiation of …Run Code Online (Sandbox Code Playgroud) 我有一堂课
class A
{
private:
std::vector< std::vector<int> > v;
//other statements
}
Run Code Online (Sandbox Code Playgroud)
我想通过将它们传递给类的构造函数来初始化这个向量的两个维度,可能使用初始化列表。
This question询问整数向量的相同问题,这询问向量向量的初始化,但在任何类之外。我想初始化两个维度的大小,但向量是一个类成员。
我怎样才能做到这一点?
编写一个接口,我必须将 的实例转换std::vector<double>为boost::array<double,N>. 每次,通过构造(没有错误),我确定向量具有正确的大小。
这是我目前正在做的一个例子(我有大约 100 个这样的函数要写在界面中):
double foo1(const std::vector<double>& x)
{
assert(x.size() == 4);
boost::array<double,4> x_;
for(std::size_t i = 0; i < 4; ++i)
x_[i] = x[i];
return foo1_(x_);
}
double foo2(const std::vector<double>& x)
{
assert(x.size() == 6);
boost::array<double,6> x_;
for(std::size_t i = 0; i < 6; ++i)
x_[i] = x[i];
return foo2_(x_);
}
Run Code Online (Sandbox Code Playgroud)
有没有更短的方法来做到这一点?
注 1:出于兼容性原因,我不使用 C++11。
注 2:我添加了标签,stdarray因为即使它是 C++11 标签,它也可能有所帮助。
请参阅以下代码片段。
根据我的理解:
a) 'p1' 和 'p2' 对象在堆栈中创建,并在 getPoints() 方法结束时销毁。
b)当使用 push_back( )将p1和p2添加到向量时,默认分配器会创建 Point 的新实例并将p1和p2的值 (x,y) 复制到这些新创建的实例中。
我的问题是:
1)我的理解正确吗?
如果是这样的话 ;
2) 如果分配器创建了新的 Point 对象,为什么我只看到两行“Points created”?
因为我希望看到p1和p2 的两行以及分配器新创建的对象的两行。
3)分配器如何为新创建的对象的 x,y 字段分配原始值?它是否使用原始内存复制?
4) 共享指针是从方法返回向量的推荐方式吗?
#include <iostream>
#include <vector>
using namespace std;
struct Point {
Point() {
std::cout<< "Point created\n";
x=0;
y=0;
}
int x;
int y;
};
std::shared_ptr< vector<Point> > getPoints() {
std::shared_ptr< vector<Point> > ret = std::make_shared< …Run Code Online (Sandbox Code Playgroud) 我目前正在利用范围构造函数std::vector来创建给定子范围的另一个向量。
std::vector<int> myVect { 1, 2, 3, 4 };
std::vector<int> subrangeVector(myVect.begin(), myVect.begin() + 2);
Run Code Online (Sandbox Code Playgroud)
但是,这会导致 myVect 的范围值被复制并占用额外的内存。当使用有限的内存和/或非常大的元素类型时,这是不可取的。
如何通过引用构建另一个向量的子范围?
我的目标的简化解释如下:
void fun(std::vector<int> & v) { v.at(0) = 1; }
int main()
{
std::vector<int> myVect { 1, 2, 3, 4 };
std::size_t upperLimit = 5;
std::vector<int> subrangeVector = subrangeView(myVect, upperLimit);
fun(subrangeVector); // so myVect.at(0) == 1
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这将在std::vector用作参数的许多不同函数中实现。我不想传递这里讨论的迭代器。假设我无法控制函数fun。
我的C ++代码中有一个具有自己的move构造函数的类。此处显示了简化版本:
class myClass {
//In this example, myClass must manually manage allocating
//and freeing a memory buffer.
char *mem;
//...
//Regular constructor, copy constructor, etc
//...
myClass(myClass &&other) {
//Swap our memory pointer with other's memory pointer
char *tmp = other.mem;
other.mem = mem;
mem = tmp;
}
//...
//Destructor, other member functions, etc.
//...
}
Run Code Online (Sandbox Code Playgroud)
在正常情况下,这可以正常工作。但是,最近我需要对这些对象进行向量处理:
vector<myClass> v;
v.reserve(10); //Make space, but do not construct
v.push_back(myClass()); //Problem!
Run Code Online (Sandbox Code Playgroud)
在经历了段错误并逐步使用gdb之后,我最终发现应该是显而易见的:如果尝试从右值引用构造对象,则可能导致在未初始化的内存上使用move构造函数。
当您有可能将垃圾交换到other类中时,应该如何编写move构造函数?有什么方法可以检测到这一点吗?
我一直在学习面向对象的计算,特别是迭代器和标准模板库等。
我似乎不太明白为什么如果你写
std:vector<int> - //blah, a vector is created.
Run Code Online (Sandbox Code Playgroud)
但是,在某些情况下,您需要编写
#include <vector> //to include vector library
Run Code Online (Sandbox Code Playgroud)
为什么是这样?我们通常编写“使用命名空间 std”的标准库是否已经包含向量库?
当我删除定义文件#include 时,计算机无法识别我的向量变量。
但是,我在某些情况下看到很多人使用了 vector 函数,而没有使用 std::vector 来实际声明它???
std::vector<int>::iterator pos;
std::vector<int>coll;
Run Code Online (Sandbox Code Playgroud)
这是其他人使用的代码,它似乎有效?
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;
int main() {
vector<int>::iterator pos;
vector<int>coll;
}
Run Code Online (Sandbox Code Playgroud)
// 这对我有用,但我想了解为什么这个有效而另一个无效。
我有 2 个像这样的向量:
std::vector<unsigned char> v1;
std::vector<unsigned char> v2;
Run Code Online (Sandbox Code Playgroud)
当然,他们每个人都有一些数据。
我知道如果我想将 v2 附加到 v1,下面是方法。v1.insert(v1.end(), v2.begin(), v2.end());
问题:
如何将 v1 加到 v2 之前?
v2.insert(v2.begin(), v1.begin(), v1.end())似乎在这里不起作用?
我知道我可能会得到使用的建议,std::deque但问题是这v2是std::vector我无法更改的遗留代码中的一个函数。它包含大量数据,我不想v2通过追加复制到这些数据。所以,我只是想加上v1,v2因为v1与v2