सी ++ का उपयोग करके, मैं एक लूप के लिए एक साथ रखने की कोशिश कर रहा हूं जो तारों के वेक्टर में सबसे बड़ा शब्द ढूंढता है। मुझे पता है कि शब्द की लंबाई की तुलना करके ऐसा करना संभव है। तो यह सिर्फ कुछ minutiae के बारे में सोच रहा है और सी ++ के बारे में कुछ चीजें सीखें, मैं यह नहीं समझ सकता कि यह क्यों काम नहीं करेगा:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::string test = "this is a test string for sentence length ranks";
std::vector<std::string> words = {"this", "is", "a", "test", "string",
"for", "sentence", "length", "ranks"};
std::string temp = "";
std::string largest = "";
for (int i = 0; i != words.size(); ++i) {
std::cout << "words[" << i << "]: " << words[i] << " ";
std:: cout << std::endl;
temp = std::max(temp, words[i]);
largest = std::max(temp, largest);
std::cout << "the biggest word is " << largest << std::endl;
}
return 0;
}
यह इसे लौटाता है:
अपडेट करें:
यहां दिए गए उत्तरों ने मुझे सही दिशा में इंगित करने में मदद की। जैसा कि यह पता चला है कि 'comp' नामक std :: max की एक और विशेषता है। मैं इसे मुश्किल से समझता हूं, लेकिन ऐसा लगता है कि यह काम करता है:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::string test = "this is a test string for sentence length ranks";
std::vector<std::string> words = {"this", "is", "a", "test", "string",
"for", "sentence", "length", "ranks"};
std::string tempword;
std::string longword;
int longwordint = 0;
for (int i = 0; i != words.size(); ++i) {
int wordlength = words[i].length();
longwordint = std::max(longwordint, wordlength);
tempword = words[i];
longword = std::max(longword, tempword,
[](std::string longword, std::string tempword)
{ return (longword.size() < tempword.size()); });
std::cout << "the biggest word so far is " << longwordint;
std::cout <<" letters long: " << longword;
std::cout << " | the tempword is: " << tempword << std::endl;
}
return 0;
}
2 जवाब
Std::max सामान्य तुलना के साथ चीजों की तुलना करता है, जो स्ट्रिंग के लिए, उनके शब्दकोश क्रम की तुलना करता है, उनकी लंबाई नहीं। तो आपके लूप में सभी परिणाम अपेक्षित हैं।
वेक्टर स्ट्रिंग में सबसे बड़ा शब्द खोजने के लिए आप std::max_element
का उपयोग कर सकते हैं।
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::string test = "this is a test string for sentence length ranks";
std::vector<std::string> words = {"this", "is", "a", "test", "string",
"for", "sentence", "length", "ranks"};
auto largest = std::max_element(words.begin(), words.end(), [](const auto& s1, const auto& s2){
return s1.size() < s2.size();
});
std::cout << "the biggest word is " << *largest << std::endl;
return 0;
}
संबंधित सवाल
नए सवाल
c++
C ++ एक सामान्य-प्रयोजन प्रोग्रामिंग भाषा है। यह मूल रूप से C के विस्तार के रूप में डिज़ाइन किया गया था और इसमें एक समान सिंटैक्स है, लेकिन यह अब पूरी तरह से अलग भाषा है। C ++ कंपाइलर के साथ संकलित कोड के बारे में प्रश्नों के लिए इस टैग का उपयोग करें। विशिष्ट मानक संशोधन [C ++ 11], [C ++ 14], [C ++ 17], [C ++ 20] या [C ++ 23], आदि से संबंधित प्रश्नों के लिए संस्करण-विशिष्ट टैग का उपयोग करें। ।