Is there a way to declare an array = a function that returns an array? (c++) [duplicate]

Let’s say I have a function that takes in an integer as an argument and returns an array that has all the digits of the integer separated as a 1D array (pseudocode):

int separatingInt (int num)
{
    int length = amount of digits in num
    int *arr = new int[length]

    for ( int i = length; i > length; i-- )
    {
        arr[i] = num%10
        num = num /10
    }

    return *arr
}

int main()
{
    ifstream file
    file.open("liczby.txt")

    int number
    file >> number

    int* arrayFromNumber = seperatingInt(int num)
}

How do I declare an array from seperatingInt() in main() (referring to the last line in the pseduocode)? Is it better to just separate the digits into an array in seperate?

I was not even sure how to formulate my question properly, so I couldn’t find answers myself.

I guess you could do it with a loop in the main() function, but since it’s reading numbers from a file, and they all have different amounts of digits, it would look messy. Maybe messy is the way to go, I don’t know.

New contributor

joler is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

10

#include <array>
#include <ranges>
#include <limits>
#include <cstdint>

using my_uint = std::uint64_t;

using digit_array =
   std::array<
      std::uint8_t,
      1 + std::numeric_limits<my_uint>::digits10
   >;

digit_array explode_digits(my_uint n){
    digit_array result{};
    for(auto& digit: result | std::views::reverse){
        /*if (n==0) //optimize for speed?
           break;*/
        digit = std::uint8_t(n % 10);
        n /= 10;
    };
    return result;
};

This is what I recommend as beginner stuff, not what academics teach.

As everyone suggests you can use std::vector, but for a 64 bit number, you only need 20 digits; It’s not so much memory that you need to store on heap.
If std::uint32_t is used, then the number of digits decreases to just 10, and for std::uint16_t, it’d be just 5.

3

For a dynamic allocation, the most common solution would use a std::vector. A simple solution would just push back the remainders into the vector and then reverse the vector:

#include <algorithm>
#include <vector>

std::vector<int> seperatingInt(int num) {
    std::vector<int> arr;              // an empty vector of ints
    const int neg = num < 0 ? -1 : 1;

    do {
        // push back the (positive) remainders, growing the vector dynamically:
        arr.push_back(neg * (num % 10));
    } while (num /= 10);

    std::reverse(arr.begin(), arr.end());

    // if num was negative, make the first digit negative:
    arr.front() *= neg; // or: arr.front() = neg * arr.front();

    return arr;
}

Example usage:

#include <climits>
#include <iostream>

int main() {
    for (int test : {INT_MIN, -123, 0, 456, INT_MAX}) {
        auto res = seperatingInt(test);

        std::cout << test << ':';

        for (int val : res) {
            std::cout << ' ' << val;
        }
        std::cout << 'n';
    }
}

Possible output:

-2147483648: -2 1 4 7 4 8 3 6 4 8
-123: -1 2 3
0: 0
456: 4 5 6
2147483647: 2 1 4 7 4 8 3 6 4 7

Yes use std::vector as shown here

#include <sstream>
#include <iostream>
#include <vector>

// When testing "files" don't use them use this instead
std::istringstream input_file{ "
1n
2n
3n
5n
8n
"};

std::vector<int> load(std::istream& is)
{
    int value;
    std::vector<int> values;
    while( is >> value )
    {
        values.push_back(value);
    }

    return values;
}

int main()
{
    //std::ifstream input_file{"filename.txt"};
    auto values = load(input_file);

    for(const int value : values) // range based for loop
    {
        std::cout << value << "n";
    }

    return 0;
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật