Bjarne Stroustrup书 - 矢量和For循环 - 将无法正常工作

Joh*_*nny 11 c++ loops vector

我在使用"使用C++编程原理和实践"学习的特定代码片段时遇到了困难.

我无法从循环中获取输出来引用向量.我正在使用std_lib_facilities和stdafx,因为这本书和MVS告诉了我.

#include "stdafx.h"
#include "../../std_lib_facilities.h"


int main()
{
    vector<string>words;
    for(string temp; cin>>temp; )
        words.push_back(temp);
    cout << "Number of words: " << words.size() << '\n';
}
Run Code Online (Sandbox Code Playgroud)

这不会产生任何结果.我会得到提示,键入一些字,然后输入,然后什么也没有.

尝试了我在这里和其他网站上的一些变化,例如:

//here i tried the libraries the guy used in his code
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    cout << "Please enter a series of words followed by End-of-File: ";
    vector<string> words;
    string word;
    string disliked = "Broccoli";

    while (cin >> word)
        words.push_back(word);

    cout << "\nNumber of words: " << words.size() << endl;

      // cycle through all strings in vector
    for (int i = 0; i < words.size(); ++i)
    {
      if (words[i] != disliked)
          cout << words[i] << endl;
      else
          cout << "BLEEP!\n";
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

依然没有.

在尝试了一些事情之后,通过消除我很确定问题在于循环到矢量通信,因为所有这些工作都很好:

int main()
{
    vector<string>words = { "hi" };
    words.push_back("hello");
    cout << words[1] << "\n"; // this will print hello

    for (int i = 0; i < words.size();++i) {
         cout << words[i] << "\n"; // this will print out all elements 
                                   // inside vector words, ( hi, hello)
         }

    cout << words.size();// this will print out number 2

    for (string temp; cin >> temp;) {
         words.push_back(temp);
        }

    cout << words.size();// this won't do anything after i type in some 
                         // words; shouldn't it increase the size of the 
                         // vector?
}
Run Code Online (Sandbox Code Playgroud)

这一点也不会:

int main()
{
    vector<string>words = { "hi" };
    for (string temp; cin >> temp;) {
        words.push_back(temp);
        }
    cout << words.size();
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?先感谢您.

小智 11

输入字符串,完成后按Ctrl+ Z(Enter如果在Windows上)或Ctrl+ D如果在Linux上.执行此操作时,循环cin>>temp;内的条件for将评估为false,程序将退出循环.


Pet*_*ter 10

首先,没有必要使用std_lib_facilities.h.这只是本书中使用的内容,以避免书中的每个示例定期包含一组标准标题,或者纠正编译器之间的非标准行为.它也是一个标题using namespace std- 你会发现很多例子(在SO和更广泛的互联网上)解释为什么using namespace std在头文件中有非常糟糕的做法.

其次,没有必要使用stdafx.h其中之一.这是由Microsoft IDE生成的,它提供了一种加速大型项目编译的方法,因为它使编译器如何使用预编译的头文件.如果您只希望使用Microsoft编译器,那么请随意填充您的靴子并使用此编译器.但是,它不是标准的C++,可能(取决于IDE和项目设置)包含不适用于非Microsoft编译器的特定于Windows的标题,并且在论坛中可能会阻止使用其他编译器的人帮助您 - 因为他们将拥有有充分的理由假设您的代码使用特定于Microsoft的扩展,这意味着他们可能无法帮助您.

第一个代码示例可以用标准C++(不使用上面的任何一个标题)重写

 #include <vector>    // for std::vector
 #include <string>    // for std::string
 #include <iostream>   // std::cout and other I/O facilities

 int main()
 {
      std::vector<std::string> words;
      for(std::string temp; std::cin >> temp; )
           words.push_back(temp);
      std::cout << "Number of words: " << words.size() << '\n';
 }
Run Code Online (Sandbox Code Playgroud)

在你兴奋之前,这会出现同样的问题(显然没有完成).原因实际上是循环的终止条件 - std::cin >> temp只有在文件结束或流中遇到其他错误时才会终止循环.

所以,如果你输入

  The cow jumped over the moon
Run Code Online (Sandbox Code Playgroud)

std::cin将继续等待输入.USER通常需要触发和结束文件条件.在Windows下,这要求用户输入CTRL-Z空行,然后输入回车键.

另一种方法是使一些预先商定的文本导致循环退出,例如

 #include <vector>    // for std::vector
 #include <string>    // for std::string
 #include <iostream>   // std::cout and other I/O facilities

 int main()
 {
      std::vector<std::string> words;
      for(std::string temp; std::cin >> temp && temp != "zzz"; )
           words.push_back(temp);
      std::cout << "Number of words: " << words.size() << '\n';
 }
Run Code Online (Sandbox Code Playgroud)

这将导致程序在输入包含单词时退出zzz.例如

 The cow jumped over the moon  zzz
Run Code Online (Sandbox Code Playgroud)

还有其他技术,例如一次读取一个字符,以及在用户输入两个连续换行符时停止.这需要您的代码解释每个字符,并决定什么构成一个单词.我会把它留作练习.

请注意,标准C++中无法直接读取击键 - 上述问题与标准流的工作方式以及与主机系统的交互有关.

用户也可以通过将相同的文本放入实际文件中"按原样"使用程序,并且(当程序运行时)程序的重定向输入来自该文件.例如 your_executable < filename.

  • *"因为他们有充分的理由假设您的代码使用特定于Microsoft的扩展,这意味着他们可能无法帮助您."*由无知的人发布的典型FUD.这可能是一个优势,因为它不鼓励无益的人试图帮助你.当然,使用预编译头文件并非*必要*,但这并不是一个错误.许多其他编译器也有类似的功能.仅仅因为你不熟悉它是如何工作的并不意味着它没用.像这样的数字作为一个有用的答案,可以解决我无法提升的问题. (4认同)