ChatCrawlersearch across public Telegram Open the app
P

Python Indonesia

сообщение · 2025-11-21 06:44 UTC
I
Replymessage unavailable
Mau tanya, apakah di python ada trik atau modul agar saat objek dari class di-instance bisa di-instance kayak list jadi: MyArray = [1,2,3] Kayak std::initializer_list di c++. Sebagai contoh: #include <iostream> #include <initializer_list> #include <cstring> using namespace std; template<typename N> class Vector { private: N* arr; int _size; public: Vector(initializer_list<N> list) { _size = list.size(); arr = new N[_size]; int i=0; for(auto val : list) { arr[i++] = val; } } ~Vector() { delete[] arr; } N at(int t) { if(t >= 0 && t < _size) { return arr[t]; } throw out_of_range("Index out of range."); } int size() { return _size; } N getmax(); N getmin(); }; template<typename N> N Vector<N>::getmin() { static N small = arr[0]; for(int j=0;j<_size;j++) { if(arr[j] < small) { return arr[j]; } } return small; } template<> const char* Vector<const char*>::getmin() { static const char* small2 = arr[0]; for(int jj=0;jj<_size;jj++) { if(strcmp(small2,arr[jj]) > 0) { return arr[jj]; } } return small2; } template<typename N> N Vector<N>::getmax() { static N big = arr[0]; for(int f=0;f<_size;f++) { if(arr[f] > big) { return arr[f]; } } return big; } template<> const char* Vector<const char*>::getmax() { static const char* big2 = arr[0]; for(int ff=0;ff<_size;ff++) { if(strcmp(big2,arr[ff]) < 0) { return arr[ff]; } } return big2; } int main(void) { Vector<int> vec = {1,2,56,71,82,98}; //begini cout << vec.at(3) << endl; cout << vec.size() << endl; cout << vec.getmin() << endl; cout << vec.getmax() << endl; return 0; }

Вся лента · оригинал в Telegram

Open in Telegram Каталог площадок Искать в ChatCrawler

A snapshot of an open public feed from the search index ChatCrawler — “Google for public Telegram”; refreshed as the venue is crawled. Times are UTC.

Public content only, official Telegram API. About · FAQ · What we do not do · Remove a page · Catalog