কম্পিউটার

একটি C/C++ স্ট্রিংয়ের শব্দগুলি পুনরাবৃত্তি করার সবচেয়ে মার্জিত উপায়


একটি C/C++ স্ট্রিং এর শব্দগুলিকে পুনরাবৃত্তি করার কোনো একটি মার্জিত উপায় নেই। সবচেয়ে পঠনযোগ্য উপায়টি কারো জন্য সবচেয়ে মার্জিত এবং অন্যদের জন্য সবচেয়ে কার্যকরী হিসাবে আখ্যায়িত করা যেতে পারে। আমি 2টি পদ্ধতি তালিকাভুক্ত করেছি যা আপনি এটি অর্জন করতে ব্যবহার করতে পারেন। প্রথম উপায় স্পেস দ্বারা পৃথক শব্দ পড়তে একটি স্ট্রিংস্ট্রিম ব্যবহার করা হয়. এটি একটু সীমিত কিন্তু কাজটি মোটামুটি ভাল করে যদি আপনি সঠিক চেক প্রদান করেন।

উদাহরণ

#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);
    }
}

  1. লিনাক্সে C++ এর সেরা IDE কি?

  2. লিনাক্সে c++ এর জন্য শীর্ষ IDE কি?

  3. উইন্ডোতে c++ এর জন্য শীর্ষ IDE কি?

  4. পাইথনে স্ট্রিংটি খালি কিনা তা পরীক্ষা করার সবচেয়ে মার্জিত উপায় কী?