标签: stdvector

向量到一维数组的向量

我想知道是否存在一种聪明的方式将信息作为一维数组呈现在向量中。示例:让我们创建一个包含5x3 int元素的向量的向量

vector< vector<int>> iVector;
ivector.resize( 5 );
for(i = 0; i < 5; i++){
    iVector[i].resize(3);
}
Run Code Online (Sandbox Code Playgroud)

但是现在我希望将此结构转换为一维数组:

int* myArray = new int[5*3];
Run Code Online (Sandbox Code Playgroud)

因此,我可以按以下方式访问我想要的每个元素:

for (i =0;i < 5; i++)
  for(j =0; j< 3; j++)
      myArray[i*3+j] = ...
Run Code Online (Sandbox Code Playgroud)

我知道我可以将向量逐个元素复制到数组,但是我想知道是否有一种方法可以直接解决向量到数组的转换问题。我也知道向量可以用来解决iVector[i][j],但不幸的是它必须是一个数组,因为它将被发送到GPU,而GPU无法理解向量。

c++ stl std stdvector

5
推荐指数
1
解决办法
4609
查看次数

如何在 std::vector 中使用空指针

假设我有一个空终止字符串向量,其中一些可能是空指针。我不知道这是否合法。这是一个学习练习。
示例代码

std::vector<char*> c_strings1;
char* p1 = "Stack Over Flow";
c_strings1.push_back(p1);
p1 = NULL; // I am puzzled you can do this and what exactly is stored at this memory location
c_strings1.push_back(p1);
p1 = "Answer";
c_strings1.push_back(p1);
for(std::vector<char*>::size_type i = 0; i < c_strings1.size(); ++i)
{
  if( c_strings1[i] != 0 )
  {
    cout << c_strings1[i] << endl;
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,即使我在位置c_strings1[1]
Question处有一个 NULL,向量的大小也是 3 。std::vector<char> 当您推送空值时,如何使用向量中到底存储了什么来 重写此代码?

编辑
我的问题的第一部分已得到彻底解答,但第二部分尚未得到解答。至少我不满意。我确实想看看vector<char>;的用法 不是一些嵌套变体或std::vector<std::string>那些熟悉的变体。所以这是我尝试过的(提示:它不起作用)

std::vector<char> c_strings2;
string …
Run Code Online (Sandbox Code Playgroud)

c++ stdvector

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

C++:如何将 std::vector 转换为 Eigen::MatrixXd?

我有std::vector<double> param(n + n * n) 长度,我需要将最后 (n * n) 个元素移动到 MatrixXd。我是这样做的:

MatrixXd cov(n, n);
for(int i = 0 ; i < n ; ++i) {
    for(int c = 0 ; c < n ; ++c) {
        cov(i, c) = param(n + i * n + c);
    }
}
Run Code Online (Sandbox Code Playgroud)

有更好的方法吗?

编辑:更好意味着更快;)

c++ stdvector eigen

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

为什么C++和Java中的动态数组具有不同的初始容量?

所以我一直在寻找动态数组的实际工作方式.我发现的是两个不同的概念.

在C++中

在C++中,动态数组通常由向量实现.向量将容量设置为0,增加计数以插入新元素,然后将新插入的容量大小加倍.

vector.h

/*
 * Implementation notes: Vector constructor and destructor
 * -------------------------------------------------------
 * The constructor allocates storage for the dynamic array and initializes
 * the other fields of the object.  The destructor frees the memory used
 * for the array.
 */

template <typename ValueType>
Vector<ValueType>::Vector() {
   count = capacity = 0;
   elements = NULL;
}
Run Code Online (Sandbox Code Playgroud)

用于扩展矢量大小

/*
 * Implementation notes: expandCapacity
 * ------------------------------------
 * This function doubles the array capacity, copies the old elements into
 * the new …
Run Code Online (Sandbox Code Playgroud)

c++ java arrays cross-language stdvector

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

按字母顺序对结构数组进行排序

我有一个名为Sheep的结构数组(A [#])(因为我的任务是关于绵羊DNR).在我做任何要求的任务后,我留下了这个结构:

struct Sheep 
{
    string Vardas;
    char Fragmentas[CMax]; 
    int atitikme = 0; 
};
Run Code Online (Sandbox Code Playgroud)

在我的内部,数据是:

(string Vardas) | (char Fragmentas[CMax]) | (int atitikme)
Baltukas   TAGCTT 3
Bailioji   ATGCAA 3 
Smarkuolis AATGAA 1 
Run Code Online (Sandbox Code Playgroud)

(char Fragmentas [CMax]将不会使用,所以你不必看它,我只是将它命名为明确).

所有这些数据都来自U2.txt文件,无法在代码中手动输入.

它剩下要做的就是按照这些规则对它进行排序:

  1. 它通过'int atitikme'从大到小.
  2. 如果'int atitikme'相等,那么它必须按'A [#]排序.Vardas按字母顺序排列.

要通过'int atitikme'对其进行排序,我创建了一个代码:

string q;
char w[20];
int e;
for (int o = 0; o < n-1; o++) 
{
    for (int p = o+1; p < n-1; p++) 
    {
        if (A[p].atitikme > A[o].atitikme) 
        {
            // - Vardo Keitimas
            q …
Run Code Online (Sandbox Code Playgroud)

c++ sorting algorithm stdvector c++11

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

为什么 std::equality_comparable 不适用于 std::vector

https://godbolt.org/z/P97MaK

我正在研究概念并期望 std::is_equality_comparable 适用于向量,但事实并非如此。

#include <type_traits>
#include <vector>
#include <concepts>


struct X {};

template<std::equality_comparable T>
bool foo( T v){
    return v == v;
}

int main()
{
    foo(10);
    foo(std::vector<X>{});
    
}
Run Code Online (Sandbox Code Playgroud)

编译错误foo在该概念保护的内部而不是在函数边界处失败。

在此输入图像描述

这是错误还是预期行为?

c++ stdvector c++-concepts c++20

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

数组向量无法编译

这个简单的程序

#include <vector>

int main()
{
    using int3 = int[3];
    std::vector<int3> vec( 2 );
}
Run Code Online (Sandbox Code Playgroud)

无法在最新的 Visual Studio 2019 16.10.0 中使用stdcpplatestswitch 进行编译,从而产生错误:

>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xutility(144,1): error C2440: 'return': cannot convert from 'int *' to '_Ty (*)'
1>        with
1>        [
1>            _Ty=int [3]
1>        ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xutility(144,48): message : Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xmemory(707): message : see reference to function …
Run Code Online (Sandbox Code Playgroud)

c++ arrays stdvector visual-studio-2019

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

附加到联合向量

我有一个工会,定义如下:

union CellType {
    std::string str;
    int i;
    double d;
    Money<2> m;  // Custom class for fixed-decimal math.
};
Run Code Online (Sandbox Code Playgroud)

然后,我就有了这样的联合的向量。

std::vector<CellType> csvLine;
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何将值附加到向量的末尾?我通常使用字符串、整数等的push_backfor vector,但是当向量元素是并集时无法完全弄清楚语法。任何帮助,将不胜感激。

(是的,这个类一次读取一行 CSV 文件,如果这有什么区别的话。)

这是失败的 MRE

#include <vector>
#include <iostream>


union CellType {
    std::string str;
    int i;
    double d;
//    Money<2> m;  // Custom class for fixed-decimal math.
};
int main() {
    std::vector<CellType> csvLine;
    CellType c;
    c.str = "foo";
    csvLine.push_back(c);
}
Run Code Online (Sandbox Code Playgroud)

Money 被注释掉是因为与 MRE 无关

错误

严重性代码说明项目文件行抑制状态错误 C2280 'CellType::CellType(void)':尝试引用已删除的函数 ConsoleApplication1 C:\work\ConsoleApplication1\ConsoleApplication1.cpp 22

c++ stdvector unions

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

std::vector&lt;int&gt; 上的调整大小是否将新元素设置为零?

考虑

#include <vector>
int main()
{
    std::vector<int> foo;
    foo.resize(10);
    // are the elements of foo zero?
}
Run Code Online (Sandbox Code Playgroud)

元素foo全为零吗?我认为它们是从 C++11 开始的。但想确切地知道。

c++ integer stdvector value-initialization c++11

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

如果可能的话, std::vector 复制赋值运算符会避免释放和重新分配内存吗?

我有一个函数将一个向量复制到另一个向量(以及许多其他东西)。代码的简化版本如下。

void Fun(std::vector<double> &in, std::vector<double> &out) {
    out = in;
}
Run Code Online (Sandbox Code Playgroud)

我关心最大化效率,因为该函数将运行多次。因此,我想尽可能避免重新分配内存。向量“in”可能已经为其保留了大量内存。因此,如果我进行手动实施,我确信我可以完成此任务。例如:

void Fun(std::vector<double> &in, std::vector<double> &out) {
    out.resize(in.size());//note - if the capacity of out is greater than in.size(), this will involve no memory release or allocation
    for (unsigned int i = 0;i<in.size();i++) {
        out[i] = in[i];
    }
}
Run Code Online (Sandbox Code Playgroud)

如果 in.size() 小于 out 的现有容量,则后一个实现将不会释放或分配任何新的堆内存。

问题是 - 我原来的实现会做同样的事情吗?即,如果我简单地执行“out = in;”,std::vector 会知道以内存有效的方式自动执行此操作吗?

我尤其担心也许“out = in;” 行可能会执行一些操作,例如释放当前分配给 out 的所有堆内存,然后重新分配它。与此有效等效的东西:

void Fun(std::vector<double> &in, std::vector<double> &out) {
    out.clear();
    out.shrink_to_fit();//releasing memory from heap
    out.resize(in.size());//reallocating memory from …
Run Code Online (Sandbox Code Playgroud)

c++ performance stdvector

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