简体   繁体   English

为什么我不能找到一个字符串?

[英]Why I cannot cout a string?

Why I cannot cout string like this:为什么我不能cout这样计算string

string text ;
text = WordList[i].substr(0,20) ;
cout << "String is  : " << text << endl ;

When I do this, I get the following error:当我这样做时,我收到以下错误:

Error 2 error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**错误 2 错误 C2679:二进制“<<”:未找到采用“std::string”类型右侧操作数的运算符(或没有可接受的转换) c:\users\mollasadra\documents\visual studio 2008\项目\barnamec\barnamec\barnamec.cpp 67 barnamec**

It is amazing, that even this is not working:令人惊讶的是,即使这样也不起作用:

string text ;
text = "hello"  ;
cout << "String is  : " << text << endl ;

You need to include你需要包括

#include <string>
#include <iostream>

You need to reference the cout's namespace std somehow.您需要以某种方式引用 cout 的命名空间std For instance, insert例如,插入

using std::cout;
using std::endl;

on top of your function definition, or the file.在您的 function 定义或文件之上。

There are several problems with your code:您的代码有几个问题:

  1. WordList is not defined anywhere. WordList没有在任何地方定义。 You should define it before you use it.你应该在使用它之前定义它。
  2. You can't just write code outside a function like this.您不能像这样在 function 之外编写代码。 You need to put it in a function.您需要将其放入 function 中。
  3. You need to #include <string> before you can use the string class and iostream before you use cout or endl .在使用coutendl之前,您需要先#include <string>才能使用字符串 class 和 iostream 。
  4. string , cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first. stringcoutendl存在于std命名空间中,因此如果不使用std::前缀就无法访问它们,除非您使用using指令首先将它们带入 scope 。

Above answers are good but If you do not want to add string include, you can use the following以上答案很好,但如果您不想添加字符串包含,您可以使用以下

ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();

return os;
}

Use c_str() to convert the std::string to const char *.使用 c_str() 将 std::string 转换为 const char *。

cout << "String is  : " << text.c_str() << endl ;

You do not have to reference std::cout or std::endl explicitly.您不必明确引用std::coutstd::endl
They are both included in the namespace std .它们都包含在namespace std中。 using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner. using namespace std而不是使用 scope 解析运算符::每次都更容易和更清洁。

#include<iostream>
#include<string>
using namespace std;

If you are using linux system then you need to add如果您使用的是 linux 系统,那么您需要添加

using namespace std;

Below headers下面的标题

If windows then make sure you put headers correctly #include<iostream.h>如果 windows 则确保正确放置标题#include<iostream.h>

#include<string.h>

Refer this it work perfectly.参考这个它完美地工作。

#include <iostream>
#include <string>

int main ()
{
std::string str="We think in generalities, but we live in details.";
                                       // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr (3,5);     // "think"

   std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr (pos);     
// get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM