Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C++,试图用for循环和STD::max找到向量中最大的单词 使用C++,我试图把一个for循环集合,它找到字符串向量中最大的单词。我知道通过比较字长可以做到这一点。所以这只是想知道一些细节,学习一些关于C++的东西,我不明白为什么这不能用: #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; }_C++_String_For Loop_Vector_Max - Fatal编程技术网

使用C++,试图用for循环和STD::max找到向量中最大的单词 使用C++,我试图把一个for循环集合,它找到字符串向量中最大的单词。我知道通过比较字长可以做到这一点。所以这只是想知道一些细节,学习一些关于C++的东西,我不明白为什么这不能用: #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; }

使用C++,试图用for循环和STD::max找到向量中最大的单词 使用C++,我试图把一个for循环集合,它找到字符串向量中最大的单词。我知道通过比较字长可以做到这一点。所以这只是想知道一些细节,学习一些关于C++的东西,我不明白为什么这不能用: #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; },c++,string,for-loop,vector,max,C++,String,For Loop,Vector,Max,它返回以下内容: 更新: 这里的答案为我指明了正确的方向。事实证明,std::max还有另一个称为“comp”的特性。我几乎无法理解,但这似乎有效: #include <iostream> #include <vector> #include <algorithm> int main() { std::string test = "this is a test string for sentence length ranks";

它返回以下内容:

更新:

这里的答案为我指明了正确的方向。事实证明,std::max还有另一个称为“comp”的特性。我几乎无法理解,但这似乎有效:

#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;
}
可以使用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"};

    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;
}
可以使用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"};

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

max将事物与普通比较进行比较,普通比较是比较字符串的字典顺序,而不是长度。因此,循环中的所有结果都是预期的。

std::max将事物与普通比较进行比较,普通比较是比较字符串的字典顺序,而不是长度。因此,循环中的所有结果都符合预期。

无需编写循环。你可以简单地使用。另外,您实际在哪里使用每个单词的大小?std::maxstring1,string2本身会给出两个字符串中较大的一个。这就是为什么我不能理解为什么循环不起作用。程序完全按照你写的那样做了-那就是得到最多两个字符串。对于字符串,最大值表示为哪个字符串按顺序排列在另一个字符串之后。除非你告诉max,否则max不会明白你真正指的是字符串的长度。你完全正确,我误解了为什么我的std::maxstring1,string2小测试能正常工作。这都是因为这个循环使用了向量。谢谢不需要编写循环。你可以简单地使用。另外,您实际在哪里使用每个单词的大小?std::maxstring1,string2本身会给出两个字符串中较大的一个。这就是为什么我不能理解为什么循环不起作用。程序完全按照你写的那样做了-那就是得到最多两个字符串。对于字符串,最大值表示为哪个字符串按顺序排列在另一个字符串之后。除非你告诉max,否则max不会明白你真正指的是字符串的长度。你完全正确,我误解了为什么我的std::maxstring1,string2小测试能正常工作。这都是因为这个循环使用了向量。谢谢谢谢,这比for循环好得多。尽管如此,我还是想知道为什么循环不起作用将值与字符串长度进行比较。谢谢,这比for循环好得多。尽管如此,我还是想知道为什么循环不起作用将值与字符串长度进行比较。谢谢!这解释了为什么我的独立std:maxstring1,string2测试有效,但向量测试无效。谢谢!这解释了为什么我的独立std:maxstring1、string2测试有效,而向量测试无效。