标签: stdvector

stl向量如何提供随机访问

昨天晚上我使用std :: vector进行工作,这个问题突然出现在我脑海中:矢量如何提供随机访问?

我试图查看代码,但没有成功.任何人都可以提供一些指示吗?

谢谢,阿伦

c++ stl stdvector

4
推荐指数
2
解决办法
6059
查看次数

矢量迭代器不兼容

我目前正在为C++开发一个图形库,现在卡在运行时调试模式中出现断言错误的位置.我也在这里看了一些关于SO的问题,但没有一个问题和答案引导我找到解决方案.在一些论坛中阅读后,我发现这个错误的发生是因为一旦矢量内容发生变化,迭代器就会失效.(例如在使用时erase())但是正如你在我的代码中看到的那样,我不是在修改向量,只是迭代.

错误出现在我标记的行中//ASSERTION.奇怪的是,neighbor_it它并没有指向第一个对象,(*vertex_it)->neighbors(而是指向0xfeeefeee.通过代码调试时,我可以清楚地看到neighbors-vector包含至少一个项目.不应该neighbor_it指向此向量中的第一个对象?

有关详细信息:m_vertices是图形中所有顶点的向量,并vertex::neighbors()返回边向量(其具有指向邻居/目标顶点的指针).在这个方法中,我想删除指向某个顶点的所有边.如果找到并删除了相应边,则返回true;如果没有指向边,则返回false p_vertex.

bool graph::remove_edges_pointing_to( vertex* p_vertex )
{
    bool res = false;

    std::vector<vertex*>::iterator vertex_it = m_vertices.begin();

    // iterate through all vertices
    while( vertex_it != m_vertices.end() )
    {
        // iterator on first element of neighbors of vertex
        std::vector<edge*>::iterator neighbor_it = (*vertex_it)->neighbors().begin();

        // iterate through all successors of each vertex
        while( neighbor_it != (*vertex_it)->neighbors().end() ) //ASSERTION
        {
            if( (*neighbor_it)->dest() == p_vertex )
            { …
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl vector stdvector

4
推荐指数
1
解决办法
7415
查看次数

如何在运行时确定矢量大小?

在运行时,可以确定一个vector?的大小?

例如

输入:25 //显示矢量大小

  code :
        int N ;
        cin << N ;
        vector <int> data[N];
Run Code Online (Sandbox Code Playgroud)

c++ vector stdvector c++-standard-library

4
推荐指数
2
解决办法
2万
查看次数

重载*,+,-'vector &lt;double&gt;类的运算符

我正在编写Line类来制作数值方法,并且希望这些运算符(*,+,-)使我的代码更易读和更易于理解。

        #include <vector>

        using namespace std;

        typedef vector<double> Vector;

        class Line : public Vector
        {
        public:
            Line();
            ~Line();

            Line operator+(Line);
            Line operator-(Line);
            Line operator*(double);
        };


        Line Line::operator*(double alfa)
        {
            Line temp;
            int n = size();
            temp.resize(n);
            for (int i = 0; i < n; i++)
            {
                temp.at(i) = this->at(i)*alfa;
            }
            return temp;
        }

        Line Line::operator+(Line line)
        {
            int n = size();
            Line temp;
            temp.resize(n);
            for (int i = 0; i < n; i++)
            {
                temp.at(i) = this->at(i) + line[i];
            } …
Run Code Online (Sandbox Code Playgroud)

c++ class operator-overloading stdvector

4
推荐指数
1
解决办法
2万
查看次数

C++将pair <int,int>的向量复制到向量<int>

我有一个对的向量,我需要将它们线性复制到一个向量的向量.我有以下代码,它运行良好,但我不确定在C++中考虑struct padding问题是否安全.

std::vector < std::pair<int, int> > test_vector;
for (int i=0;i<5;i++) {
    test_vector.push_back(std::make_pair(i,i*5));
}
std::vector<int> int_vec(test_vector.size() * 2);
std::copy(reinterpret_cast<int*>(&(*test_vector.begin())),reinterpret_cast<int*>(&(*test_vector.end())),int_vec.begin());
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是 - 上述代码是否安全?如果没有,是否有一种优雅的方式来做而不编写循环?

c++ struct stl stdvector std-pair

4
推荐指数
1
解决办法
3902
查看次数

为什么C++分配器会避免就地重新分配

我对realloc的理解是,如果内存连续超出分配点,它可以尝试扩展当前分配而不复制.

在阅读此https://github.com/facebook/folly/blob/master/folly/docs/FBVector.md时,我发现大多数分配器都避免了就地重新分配

.许多内存分配器不支持就地重新分配,尽管大多数内存分配器都可以.这来自现在臭名昭着的realloc()设计,它不透明地执行就地重新分配或分配memcpy-deallocate循环.这种缺乏控制随后迫使所有基于clib的分配器设计避免就地重新分配,包括C++的new和std:allocator.

正如在另一个问题(为什么C++分配器中没有重新分配功能?)中所回答的那样,C++中缺少重新分配器,其中接受的答案提到这样的分配器会禁止重新分配使用C库但不回答为什么不尝试制作realloc如果可能扩展当前内存?

c++ memory memory-management stdvector

4
推荐指数
2
解决办法
489
查看次数

对std :: vector :: iterator使用vs. typedef

我在使用新的C++ 11 using关键字时遇到问题.据我所知,它是别名typedef.但我无法编译.我想为a的迭代器定义一个别名std::vector.如果我使用它,一切都很完美.

typedef std::vector<fix_point>::iterator inputIterator;
Run Code Online (Sandbox Code Playgroud)

但如果我尝试:

using std::vector<fix_point>::iterator = inputIterator;
Run Code Online (Sandbox Code Playgroud)

代码不能编译:

Error: 'std::vector<fix_point>' is not a namespace
using std::vector<fix_point>::iterator = inputIterator;
                            ^
Run Code Online (Sandbox Code Playgroud)

为什么不编译?

c++ using stdvector c++11

4
推荐指数
2
解决办法
873
查看次数

Vector容器中的C++智能指针

我编写了经典的Shape Polymorphism代码,但有点不同,因为我使用的是Vector容器和智能指针.

我不是C++专家,我想知道以下内容:

  1. 有内存泄漏吗?
  2. 如果我不打电话shapes.clear(),会不会有内存泄漏?
  3. 有没有更好的方法来使用智能指针和容器?

码:

#include <iostream>
#include <memory>
#include <vector>
using namespace std;

class Shape {
  public:
    virtual float getArea() = 0;
    virtual ~Shape() {}
};

class Rectangle : public Shape {
  public:
    Rectangle(float w, float h) : width(w), height(h) {}

    float getArea() {
      return width * height;
    }
  private:
    float width;
    float height;
};

class Circle : public Shape {
  public:
    Circle(float r) : radius(r) {}

    float getArea() {
      return 3.141592653589793238462643383279502884 * radius * …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism smart-pointers stdvector

4
推荐指数
2
解决办法
4017
查看次数

用等距值填充 std::vector&lt;double&gt; 的最简单方法

假设我有价值观double start,double end和步长double step。

填充std::vector<double>从 at 开始start并stepsize随着当前值小于递增的值的最简单方法是end什么?

我在问自己,是否有一个 stl 函数使这个任务成为单线任务。

std::vector<double> fill(double start, double end, double step) {
  // Code
}

main() {
  auto ret=fill(0.2, 2.3, 0.2);
  // ret = {0.2, 0.4, 0.6, ... , 2.2}
}
Run Code Online (Sandbox Code Playgroud)

c++ stl stdvector

4
推荐指数
1
解决办法
1607
查看次数

c ++ float*array作为对std :: vector <float>的引用

我想创建一个std::vector<float> vpd将引用的float*.

float * old = new float[6];
for (int i = 0; i < 6; i++)
{
    old[i] = i;
}
vector<float> vpd(6);
auto refasd = &*vpd.begin();
*refasd = *old;
vpd[0] = 23;
cout << old[0] << endl;
Run Code Online (Sandbox Code Playgroud)

我应该如何修改代码,如果我想要得到23的cout?

c++ arrays reference stdvector

4
推荐指数
2
解决办法
570
查看次数