【问题标题】:C++ Program Apparently Printing Memory Address instead of ArrayC ++程序显然打印内存地址而不是数组
【发布时间】:2011-04-10 08:57:04
【问题描述】:
#include <iostream>
using namespace std;

int main(){
    int findMax(int *);

    const int MAX = 100;
    int values[MAX];
    char ivals[256];
    // Get the space-separated values from user input.
    cin.getline(ivals, 256, '0');
    char *helper;
    // Clean input array and transfer it to values.
    for(int i = 0; i < (MAX) && ivals[i] != 0; i++){
        helper = ivals[i * 2];
            values[i] = atoi(helper);

    }

    int mval = findMax(values);
    cout << values << endl << mval;
    return 0;
}
//Function to find the maximum value in the array
int findMax(int arr[]){
    int localmax = 0;
    for(int i = 0; i < (sizeof(arr)/sizeof(int)); i++){
        if(arr[i] > localmax){
            localmax = arr[i];
        }
    }
    return localmax;
}

该程序的目的是让用户输入以 0 结尾的以空格分隔的一系列值。然后分析该数组以找到最大值。我想出了如何将最初的 char[] 转换为 int[] 以便我可以在其上使用 findMax() 函数而不会出错,但排序循环似乎有其自身的问题,当“cout

【问题讨论】:

标签: c++ arrays memory printing


【解决方案1】:

当你传递一个 X 数组时,它实际上是一个指向你正在传递的 X 数组的指针。因此,当您将 values 传递给 cout 时,它只有要打印的指针。

您确实应该考虑使用一些标准算法来简化您的生活。

例如,要打印数组中的所有元素,您只需编写即可

std::copy(values, values+MAX, std::ostream_iterator<int>(std::cout, "\n"));

要找到你可以写的最大元素

int mval = *std::max_element(values, values+MAX);

所以你的代码变成了

#include <iostream>
using namespace std;

int main(){

    const int MAX = 100;
    int values[MAX];
    char ivals[256];
    // Get the space-separated values from user input.
    cin.getline(ivals, 256, '0');
    char *helper;
    // Clean input array and transfer it to values.
    for(int i = 0; i < (MAX) && ivals[i] != 0; i++){
        helper = ivals[i * 2];
            values[i] = atoi(helper);

    }

    copy(values, values+MAX, ostream_iterator<int>(cout, "\n"));
    cout << *std::max_element(values, values+MAX);
    return 0;
}

这样做完全不需要您的 findMax 方法。

我还会重新编写您的代码,以便您使用向量而不是数组。这使您的代码更短。并且可以使用 stringstream 将字符串转换为数字。

这样的东西应该可以工作,并且比原来的代码少很多。

int main(){


    vector<int> values;
    char ivals[256];

    // Get the space-separated values from user input.
    cin.getline(ivals, 256, '0');

    int temp = 0;
    stringstream ss(ivals);
    //read the next int out of the stream and put it in temp
    while(ss >> temp) {
        //add temp to the vector of ints
        values.push_back(temp);
    }

    copy(values.begin(), values.end(), ostream_iterator<int>(cout, "\n"));
    cout << *std::max_element(values.begin(), values.end());
    return 0;
}

【讨论】:

  • +1 for std::max_element -- 无论如何,这段代码首先应该是这样写的。
【解决方案2】:

打印values 不会像你期望的那样打印数组的内容,它会打印数组第一个元素的内存位置。

试试这样的方法:

#include <iterator>
#include <algorithm>

// ...

copy(&values[0], &values[MAX], ostream_iterator(cout, " "));

抱歉,我无法发布实际的工作代码,但您的原始帖子包含许多语法和句法错误。

编辑:为了让初学者更完整、更容易理解和理解,我编写了一个小程序,说明了完成此任务的 4 种方法。

方法 1 使用 copyostream_iterator,就像我在上面所做的那样。 下面的方法 2 可能是最基本和最容易理解的。 方法 3 是 C++0x 方法。我知道这个问题被标记为 C++,但我认为添加它可能具有教育意义。 方法 4 是使用 vectorfor_each 的 C++ 方法。我已经实现了一个进行转储的函子。

分享和享受

#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;

struct dump_val : public unary_function<int,void>
{
    void operator()(int val)
    {
        cout << val << " ";
    }
};

int main(){
    int vals[5] = {1,2,3,4,5};


    // version 1, using std::copy and ostream_iterator
    copy(&vals[0], &vals[5], ostream_iterator<int>(cout, " "));
    cout << endl;

    // version 2, using a simple hand-written loop
    for( size_t i = 0; i < 5; ++i )
        cout << vals[i] << " ";
    cout << endl;

    // version 3, using C++0x lambdas
    for_each(&vals[0], &vals[5], [](int val) 
    {
        cout << val << " ";
    }
    );
    cout << endl;

    // version 4, with elements in a vector and calling a functor from for_each
    vector<int> vals_vec;
    vals_vec.push_back(1);
    vals_vec.push_back(2);
    vals_vec.push_back(3);
    vals_vec.push_back(4);
    vals_vec.push_back(5);
    for_each( vals_vec.begin(), vals_vec.end(), dump_val() );
    cout << endl;

}

【讨论】:

  • +1 用于使用标准库,但这对于初学者来说可能不是最简单的方法:)
  • 取决于您对“简单”的定义。 :) 就我个人而言,我通常发现使用 STL 的算法比制作手写循环更容易和更简单。我承认很多 C++ 程序员没有意识到 copy 可以以这种方式使用。
【解决方案3】:
for(int i = 0; i < (sizeof(arr)/sizeof(int)); i++){

sizeof(arr) 这里是指向数组的指针的大小。 C++ 不会传递实际的数组,那将是非常低效的。您通常只会通过循环一次。像这样声明你的函数:

int findMax(int* arr, size_t elements) {
    //...
}

但是,真的,使用矢量。

哦,等一下,这个问题。循环遍历数组并打印每个单独的元素。

【讨论】:

    【解决方案4】:

    当传递给函数时,int 数组被提升为指向 int 的指针。没有运算符

    注意:当使用模板传递给函数时,技术上可以区分数组,但这不适用于标准运算符

    【讨论】:

      猜你喜欢
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 2020-04-23
      • 2015-09-21
      相关资源
      最近更新 更多