পদ্ধতি ১: C-স্টাইল itoa() (নন-স্ট্যান্ডার্ড)
#include <iostream>
#include <cstdlib> // itoa জন্য
using namespace std;
int main() {
int a = 10;
char buffer[20];
itoa(a, buffer, 10); // base 10
string str = buffer;
cout << str;
}
পদ্ধতি ২: C++11 to_string() (স্ট্যান্ডার্ড, সুপারিশকৃত)
#include <iostream>
#include <string>
using namespace std;
int main() {
int a = 10;
string str = to_string(a);
cout << str;
}
আউটপুট
10
to_string সমস্ত ইন্টিজার টাইপ (int, long, long long, unsigned ইত্যাদি) এবং ফ্লোটিং পয়েন্ট টাইপের জন্য ওভারলোডেড। এটি লোকেল-স্বাধীন এবং নিরাপদ।
বিকল্প: std::ostringstream বা std::format (C++20) ব্যবহার করেও করা যায়।