Printing an array in C++?

前端 未结 10 1226
谎友^
谎友^ 2020-11-27 14:35

Is there a way of printing arrays in C++?

I\'m trying to make a function that reverses a user-input array and then prints it out. I tried Googling this problem and i

相关标签:
10条回答
  • 2020-11-27 14:52

    There are declared arrays and arrays that are not declared, but otherwise created, particularly using new:

    int *p = new int[3];
    

    That array with 3 elements is created dynamically (and that 3 could have been calculated at runtime, too), and a pointer to it which has the size erased from its type is assigned to p. You cannot get the size anymore to print that array. A function that only receives the pointer to it can thus not print that array.

    Printing declared arrays is easy. You can use sizeof to get their size and pass that size along to the function including a pointer to that array's elements. But you can also create a template that accepts the array, and deduces its size from its declared type:

    template<typename Type, int Size>
    void print(Type const(& array)[Size]) {
      for(int i=0; i<Size; i++)
        std::cout << array[i] << std::endl;
    }
    

    The problem with this is that it won't accept pointers (obviously). The easiest solution, I think, is to use std::vector. It is a dynamic, resizable "array" (with the semantics you would expect from a real one), which has a size member function:

    void print(std::vector<int> const &v) {
      std::vector<int>::size_type i;
      for(i = 0; i<v.size(); i++)
        std::cout << v[i] << std::endl;
    }
    

    You can, of course, also make this a template to accept vectors of other types.

    0 讨论(0)
  • 2020-11-27 14:58

    Use the STL

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    
    int main()
    {
        std::vector<int>    userInput;
    
        // Read until end of input.
        // Hit control D  
        std::copy(std::istream_iterator<int>(std::cin),
                  std::istream_iterator<int>(),
                  std::back_inserter(userInput)
                 );
    
        // Print in Normal order
        std::copy(userInput.begin(),
                  userInput.end(),
                  std::ostream_iterator<int>(std::cout,",")
                 );
        std::cout << "\n";
    
        // Print in reverse order:
        std::copy(userInput.rbegin(),
                  userInput.rend(),
                  std::ostream_iterator<int>(std::cout,",")
                 );
        std::cout << "\n";
    
        // Update for C++11
        // Range based for is now a good alternative.
        for(auto const& value: userInput)
        {
            std::cout << value << ",";
        }
        std::cout << "\n";
    }
    
    0 讨论(0)
  • 2020-11-27 14:58

    C++ can print whatever you want if you program it to do so. You'll have to go through the array yourself printing each element.

    0 讨论(0)
  • 2020-11-27 15:01

    This might help //Printing The Array

    for (int i = 0; i < n; i++)
    {cout << numbers[i];}
    

    n is the size of the array

    0 讨论(0)
  • 2020-11-27 15:02

    Most of the libraries commonly used in C++ can't print arrays, per se. You'll have to loop through it manually and print out each value.

    Printing arrays and dumping many different kinds of objects is a feature of higher level languages.

    0 讨论(0)
  • 2020-11-27 15:04

    It certainly is! You'll have to loop through the array and print out each item individually.

    0 讨论(0)
提交回复
热议问题