标签: stdvector

使用连续无符号整数列表初始化std :: vector <unsigned int>

我想使用一种特殊的方法来初始化一个std::vector<unsigned int>我在其中作为参考的C++书籍中描述的内容(德国着作"Ulrich Breymann的Der C++ Programmer",如果重要的话).在这本书是关于序列类型的STL的部分,特别是指list,vector和deque.在本节中,他写道,这种序列类型有两个特殊的构造函数,即,如果X引用这样的类型,

X(n, t) // creates a sequence with n copies of t
X(i, j) // creates a sequence from the elements of the interval [i, j)
Run Code Online (Sandbox Code Playgroud)

我希望使用第二个间隔unsigned int,即

std::vector<unsigned int> l(1U, 10U);
Run Code Online (Sandbox Code Playgroud)

获取初始化的列表{1,2,...,9}.然而,我得到的是一个unsigned int值为10 的向量: - | 第二个变体是否存在,如果存在,我该如何强制它被调用?

c++ stl initialization std stdvector

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

如何在没有内存操作的情况下在C++和STL中定义2D数组?

有几种方法可以在没有内存操作的情况下在C++和STL中定义2D数组,以下代码说明了两种不同的方法:

int main () 
{
    /**************
        1   2   3
        4   5   6
    ***************/
    // Method 1
    const int ROW = 2;
    const int COL = 3;
    int array1[ROW][COL];
    for(int i=0; i<ROW; i++)
        for(int j=0; j<COL; j++)
            array1[i][j] = i*COL+j+1;

    // Method 2
    typedef vector<vector<int> > ARRAY; 
    ARRAY array2;
    vector<int> rowvector;
    for(int i=0; i<ROW; i++)
    {
        rowvector.clear();
        for(int j=0; j<COL; j++)
            rowvector.push_back(i*COL+j+1);
        array2.push_back(rowvector);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:还有其他方法来定义2D数组吗?哪一个最有效?谢谢!

c++ arrays stl std stdvector

9
推荐指数
5
解决办法
3万
查看次数

如何获取指向std :: vector的最后插入元素的指针?

我提出了以下剪辑,但它看起来很hacky.

vector<int> collection;
collection.push_back(42);
int *pointer = &(*(collection.end()--));
Run Code Online (Sandbox Code Playgroud)

是否有一种简单的方法来获取指向最后插入元素的指针?

c++ pointers std push-back stdvector

9
推荐指数
1
解决办法
8087
查看次数

获取向量的字节大小

对不起,这可能是简单而愚蠢的问题,但我无法在任何地方找到它.

我只是不知道如何获得std :: vector的字节大小.

std::vector<int>MyVector;   
/* This will print 24 on my system*/   
std::cout << "Size of  my vector:\t" << sizeof(MyVector) << std::endl;

for(int i = 0; i < 1000; i++)
   MyVector.push_back(i);

/* This will still print 24...*/    
std::cout << "Size of  my vector:\t" << sizeof(MyVector) << std::endl;
Run Code Online (Sandbox Code Playgroud)

那么我如何获得向量的大小?!也许将24(矢量大小)乘以项目数?

c++ byte sizeof stdvector

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

使用C++ 11中的初始化列表初始化2D向量

如何使用初始化列表初始化2D矢量?对于正常的向量做:

vector<int> myvect {1,2,3,4};
Run Code Online (Sandbox Code Playgroud)

就够了 但对于2D人来说:

vector<vector<int>> myvect{ {10,20,30,40},
                            {50,60,70,80}
                          };
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?
我怎么能用它来迭代呢?

for(auto x: myvect)
{
    cout<<x[j++]<<endl;
}
Run Code Online (Sandbox Code Playgroud)

这仅用于显示:10,1!

顺便说一下这是什么意思?

   vector<int> myvect[5] {1,2,3,4};
Run Code Online (Sandbox Code Playgroud)

我在这里看到它并且无法理解它!链接

stdvector c++11

9
推荐指数
1
解决办法
7267
查看次数

std :: vector:连续数据和复制/移动

我对以下代码有两个问题:1)元素是否faces会连续? 2)插入时是否std::vector复制或移动Face f?

#include <vector>    
int main()
{
    struct Face {};
    std::vector<Face> faces;

    for (int i=0; i<10; ++i)
    {
        Face f;

        faces.push_back (f);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ stdvector contiguous c++11

9
推荐指数
1
解决办法
1968
查看次数

如何有效地将二进制文件读入矢量C++

我需要读一个大的二进制文件(~1GB)std::vector<double>.我目前正在使用infile.read将整个事物复制到char *缓冲区(如下所示),我目前正计划将整个事物转换doubles为reinterpret_cast.肯定有一种方法可以doubles直接进入vector?

我也不确定二进制文件的格式,数据是在python中生成的,所以它可能都是浮点数

ifstream infile(filename, std--ifstream--binary);

infile.seekg(0, infile.end);     //N is the total number of doubles
N = infile.tellg();              
infile.seekg(0, infile.beg);

char * buffer = new char[N];

infile.read(buffer, N);
Run Code Online (Sandbox Code Playgroud)

c++ binary file stdvector

9
推荐指数
1
解决办法
7465
查看次数

`std :: vector <std :: unique_ptr <T>>`出错

我看到一些错误路过std::vector< std::unique_ptr< T > >周围std::move.重现问题的代码是这样的:

#include <memory>    // for std::unique_ptr
#include <utility>   // for std::move
#include <vector>    // for std::vector

struct bar {};
using vtype = std::vector<std::unique_ptr<bar>>;

struct foo
{
    foo(vtype v) : _v(std::move(v)) { }
private:
    vtype _v;
};

vtype getVector()
{
    return { std::move( std::unique_ptr<bar>(new bar()) ) };
};    

int main()
{
foo f(std::move(getVector()));
};
Run Code Online (Sandbox Code Playgroud)

使用clang 3.4,此代码会产生此错误:

$ clang++ -std=c++11 test.cpp -o xtest
In file included from test.cpp:1:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/memory:64:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/stl_construct.h:75:38: …
Run Code Online (Sandbox Code Playgroud)

c++ stdvector unique-ptr move-semantics c++11

9
推荐指数
1
解决办法
2308
查看次数

为什么与<运算符的矢量比较比较每个项目两次?

在这个例子中,比较两个向量与<operator result in operator <,在Integer类上定义,为每个元素调用两次.但是,将两个向量与==运算符进行比较时不会发生这种情况.

#include<iostream>
#include<vector>

class Integer {
    public:
        Integer(int value) : m_value(value) {}
        friend bool operator<(const Integer& lhs, const Integer& rhs);
        friend bool operator==(const Integer& lhs, const Integer& rhs);

    private:
        int m_value;

}; 
bool operator<(const Integer& lhs, const Integer& rhs) {
            std::cout << lhs.m_value << " < " << rhs.m_value << '\n';
            return lhs.m_value < rhs.m_value;
}
bool operator==(const Integer& lhs, const Integer& rhs) {
            std::cout << lhs.m_value << " == " << rhs.m_value << '\n';
            return …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading stdvector

9
推荐指数
2
解决办法
263
查看次数

迭代器或reverse_iterator的一个变量?

我想std::vector在for循环中迭代一些s,但是根据某些条件,向量应该向前或向后迭代.我想,我可以通过使用普通迭代器或反向迭代器来轻松实现,如下所示:

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

int main() {
    vector<int> vec{0, 1, 2, 3, 5, 6, 7};
    bool reverse = true;
    std::iterator<random_access_iterator_tag, int> it, end_it;

    if (reverse) {
      it = vec.rbegin();
      end_it = vec.rend();
    } else {
      it = vec.begin();
      end_it = vec.end();
    }

    for (; it != end_it; it++) {
        cout << *it << ", ";
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但不幸的是vector::begin(),vector::rbegin()似乎并没有使用相同的父类.是否有另一种方法可以做我想要的,而不在if-else结构中有两个不同的循环?当然我可以为循环体创建一个函数/ lambda或者使用一些索引算法但是有更优雅的方法吗?

编译器抱怨分配,it = vec.begin()因为它们是不同的类型.gcc和VC++输出不同的错误,似乎使用不同的类型作为返回值vector::begin.

c++ iterator for-loop stdvector reverse-iterator

9
推荐指数
0
解决办法
444
查看次数