প্রথম উপায় হল স্পেস দ্বারা আলাদা করা শব্দ পড়ার জন্য একটি স্ট্রিংস্ট্রিম ব্যবহার করা৷ এটি একটু সীমিত কিন্তু কাজটি মোটামুটি ভাল করে যদি আপনি সঠিক চেক প্রদান করেন।
উদাহরণ
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main() {
string str("Hello from the dark side");
string tmp; // A string to store the word on each iteration.
stringstream str_strm(str);
vector<string> words; // Create vector to hold our words
while (str_strm >> tmp) {
// Provide proper checks here for tmp like if empty
// Also strip down symbols like !, ., ?, etc.
// Finally push it.
words.push_back(tmp);
}
} উদাহরণ
আরেকটি উপায় হল গেটলাইন ফাংশন -
ব্যবহার করে স্ট্রিংকে বিভক্ত করার জন্য একটি কাস্টম ডিলিমিটার প্রদান করা।#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main() {
std::stringstream str_strm("Hello from the dark side");
std::string tmp;
vector<string> words;
char delim = ' '; // Ddefine the delimiter to split by
while (std::getline(str_strm, tmp, delim)) {
// Provide proper checks here for tmp like if empty
// Also strip down symbols like !, ., ?, etc.
// Finally push it.
words.push_back(tmp);
}
}