최대 1 분 소요

개요

  • array를 생성


예제

  • 코드
    #include <array>
    #include <cstring>
    #include <iostream>
    #include <string>
    #include <string_view>

    using namespace std;

    template <typename T> constexpr string_view type_name() {
    	const string s = __PRETTY_FUNCTION__;
    	const int prefixSize = s.find("[with T = ") + strlen("[with T = ");

    	return string_view(s.data() + prefixSize, s.find(';') - prefixSize);
    }

    int main() {
    	auto print = [](auto a) {
    		cout << type_name<decltype(a)>() << endl;

    		for (const auto &iter : a) {
    			cout << iter << " ";
    		}
    		cout << endl;
    	};

    	print(to_array("abc"));

    	cout << "------" << endl;

    	print(to_array({1, 2, 3}));

    	cout << "------" << endl;

    	print(to_array<double>({1, 2, 3}));

    	return 0;
    }
  • 실행 결과
    std::array<char, 4>
    a b c
    ------
    std::array<int, 3>
    1 2 3
    ------
    std::array<double, 3>
    1 2 3