插入运算符不适用于向量,我不知道为什么

Insertion operator is not working with vector and I don't know why

所以我开始编写我的代码,我打算测试看看我是否还记得如何投射,直到我的操作符下方出现一条红线。 这是编译器错误:

Error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' (or there is no acceptable conversion) (12)

老实说,我在输出 string/vector 时从来没有遇到过问题,所以我不知道如何解决这个问题。有人可以告诉我如何解决这个问题。如果你能告诉我代码有什么问题,那就太棒了。

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<string>hello;
    hello.push_back("9");
    for (auto i : hello)
        cout << i << " "; <-- The first operator is underlined. Why?
    return 0;
}

您的程序中还需要包含一个:

#include <string>

虽然 <iostream> 执行 declare/define 一些与字符串相关的功能,但不是全部。

对于某些编译器,iostream header 在内部包含字符串,但这不是标准所要求的 - 而 Visual Studio 则不需要,这就是您收到此错误的原因。