【问题标题】:Queue array pointers (C++)队列数组指针 (C++)
【发布时间】:2018-06-11 13:59:06
【问题描述】:

我是 C++ 新手。因此还有使用指针的想法。 我有一些要排队的数组。我知道我的代码在使用指针时不起作用。但是我不知道我应该如何解决这个问题? 我需要队列中的元素是 float* 类型,以便以后与 BLAS 一起使用。

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


float* initialize(float* vec, int len){
    for(int i = 0; i < len; ++i){
        vec[i] = 0;     // Initialize
    }
    return vec;
}

void printVector(float* vec, int len){
    for(int i=0;i<len;i++)
        cout << vec[i] << endl;
    cout << "--" << endl;
}

int main ()
{
    queue<float*> q;
    int len = 3;
    float* k = new float[len];
    k = initialize(k,len);

    for(int t=0;t<len;t++){
        k[t] = 1;   
        printVector(k, len);
        q.push(k);
        k[t] = 0;
    }

    // I would like the one below to give same output as above
    cout << "Should have been the same:" << endl;

    while (!q.empty()){
        printVector(q.front(), len);
        q.pop();
    }

    return 0;
}

额外的问题是这些类型的“指针数组”有一个特殊的名称吗? 浮动* k = 新浮动[len];

【问题讨论】:

  • 您可以使用std::vector&lt;float&gt;。如果您必须将 C 数组传递给 BLAS 函数,您可以使用std::vector::data()(或&amp;std::vector::operator[0])提取它们。 std::vector::size() 提供大小(元素数量)(可能也是必需的)。
  • k 只是指向新数组第一个元素的指针。它与指向任何其他 float 的指针没有区别,除非你可以/必须用它做什么。
  • 谢谢。是的,我明白这一点。但是,如果我希望 q.front() 成为 float*,我不知道如何解决这个问题。
  • @Christian 你可能不需要那个。如果你使用std::queue&lt;std::vector&lt;float&gt;&gt;,你可以很容易地使用q.front().data()
  • 一般来说,您可以按照您尝试过的方式进行操作,但是:您只分配一次存储空间。要使其正常工作,您必须在循环中分配存储空间(带有q.push(); 的存储空间)。另一个循环应该delete[]弹出的指针resp。 (否则您可能会出现内存泄漏。)正如您所做的那样,所有队列条目共享相同的存储空间(因此会产生副作用)。 => 分配指针不会复制指向的内容。

标签: c++ arrays pointers queue


【解决方案1】:

基本信息是:存储指针(指向数组或其他任何内容)不会复制内容。它只是复制它的地址。

稍微重新排序你的语句我让 OP 的示例运行(并且做可能打算做的事情):

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


float* initialize(float* vec, int len){
    for(int i = 0; i < len; ++i){
        vec[i] = 0;     // Initialize
    }
    return vec;
}

void printVector(float* vec, int len){
    for(int i=0;i<len;i++)
        cout << vec[i] << endl;
    cout << "--" << endl;
}

int main ()
{
    queue<float*> q;
    int len = 3;

    for(int t=0;t<len;t++){
        float* k = new float[len];
        k = initialize(k,len);
        k[t] = 1;   
        printVector(k, len);
        q.push(k);
    }

    // I would like the one below to give same output as above
    cout << "Should have been the same:" << endl;

    while (!q.empty()){
        printVector(q.front(), len);
        delete[] q.front();
        q.pop();
    }

    return 0;
}

输出:

1
0
0
--
0
1
0
--
0
0
1
--
Should have been the same:
1
0
0
--
0
1
0
--
0
0
1
--

在 C++ 中使用 new 容易出错,并且在许多情况下都可以避免。对std::vector&lt;float&gt; 做同样的事情,它可能看起来像这样:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

void printVector(const float* vec, int len){
    for(int i=0;i<len;i++)
        cout << vec[i] << endl;
    cout << "--" << endl;
}

int main ()
{
    queue<std::vector<float> > q;
    int len = 3;

    for(int t=0;t<len;t++){
        q.push(std::vector<float>(3, 0.0f));
        q.back()[t] = 1.0f;
        printVector(q.back().data(), q.back().size());
    }

    // I would like the one below to give same output as above
    cout << "Should have been the same:" << endl;

    while (!q.empty()){
        printVector(q.front().data(), q.front().size());
        q.pop();
    }

    return 0;
}

输出相同。

Live Demo on coliru

【讨论】:

  • 谢谢谢夫!正是我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多