अंतिम अद्यतन:2026-10-12
C++ loop के बिना 1-100 print करना¶
सबके लिए एक शर्त रखते हैं: loop उपयोग किए बिना, 1 से 100 तक के numbers print करें।
Loop उपयोग करें तो बहुत सरल है, तो आप कितने तरीके सोच सकते हैं?
आज आपको C++ की कुछ शानदार tricks से ऐसा code लिखकर दिखाते हैं, comments और share के लिए स्वागत है~
यह प्रश्न वास्तव में बहुत सरल है, पर इसमें कई interview topics शामिल हैं, जिनमें हैं:
- Template specialization, partial specialization, recursive template कैसे लिखें
- CRTP
- std::copy, std::iota, std::transform का उपयोग कैसे करें, हर algorithm का implementation
- Class के static variables
- आदि
1. Recursion¶
1-100 को 5*5*4 में तोड़ा जा सकता है, इसे print करने के लिए तीन functions उपयोग करें, function a एक बार cout करता है, फिर variable 1 बढ़ाता है, function b में a को लेयर-दर-लेयर nested रूप से 5 बार दोहराया जाता है, function c में b को लेयर-दर-लेयर nested रूप से 5 बार दोहराया जाता है, और अंत में main function में c को लेयर-दर-लेयर 4 बार nested रूप से चलाया जाता है।
#include <iostream>
int a(int n) {
std::cout << n++ << std::endl;
return n;
}
int b(int n) { return a(a(a(a(a(n))))); }
int c(int n) { return b(b(b(b(b(n))))); }
int main() {
c(c(c(c(1))));
return 0;
}
2. Template overloading¶
Templates और specialization से handle करें, जब iteration 100 पहुंचे तो केवल 100 output करें, अन्यथा I output करें, और +1 करके recursively process करें।
#include <iostream>
template <int I>
struct JustPrint : public JustPrint<I + 1> {
JustPrint() : JustPrint<I + 1>() { std::cout << I << " "; }
};
template <>
struct JustPrint<100> {
JustPrint<100>() { std::cout << 1 << " "; }
};
int main() { JustPrint<1> j; }
3. CRTP¶
तरीका 3, तरीका 2 पर आधारित है, उसे crtp programming style में बदला गया है, थोड़ा समझ नहीं आ रहा न, हीही।
नीचे के struct को class से बदला जा सकता है, यहाँ मैं class से describe करता हूँ। वास्तव में ये दो classes हैं, पहली class Print parent class है, हर बार का I print करती है, और child class को call करती है, child class PrintDerived, Print को inherit करती है, और खुद को parent class को पास करती है, child class एक overloaded class है, जो recursion के termination को control करती है।
#include <iostream>
template <typename Derived, int I>
struct Print {
Print() {
std::cout << I << " ";
Derived::next();
}
};
template <int I>
struct PrintDerived : public Print<PrintDerived<I>, I> {
static void next() { PrintDerived<I + 1>(); }
};
template <>
struct PrintDerived<100> : public Print<PrintDerived<100>, 100> {
static void next() {}
};
int main() {
PrintDerived<1> j;
return 0;
}
4. Class के static variables¶
एक Test array बनाएँ, जिसमें 100 elements हों, static variable हर बार accumulate होता है।
#include <iostream>
using namespace std;
class Test {
static int i;
public:
Test() { cout << ++i << endl; }
};
int Test::i;
int main() {
Test a[100];
return 0;
}
5. Algorithms library का उपयोग¶
std::for_each उपयोग करें, सीधा-सादा।
int main() {
std::vector<int> l(100);
std::iota(l.begin(), l.end(), 1);
std::for_each(l.begin(), l.end(), [](int& n) { std::cout << n << std::endl; });
return 0;
}
6. C++20 ranges¶
C++20 के std::views उपयोग करें, std::copy के समय print करें।
#include <iostream>
#include <iterator>
#include <ranges>
int main() {
auto numbers = std::views::iota(1, 100 + 1);
std::copy(numbers.begin(), numbers.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
इस तरीके को आगे बदलकर एक और implementation है, std::transform उपयोग करके।
auto range = std::views::transform(std::ranges::iota_view{1, 101}, [](const auto& x) {
std::cout << x << std::endl;
return x;
});
std::vector<int> a(range.begin(), range.end());
संबंधित विषय¶
- C++ string के छह split तरीके | C++那些事
- CPython extension development notes | C++那些事
- libcurl C++ development notes | C++那些事
- Open source C++ code कैसे पढ़ें | C++那些事
- एक दिलचस्प C++ code | C++那些事