cout有关头文件

#include<stdio.h>
#include<vector>
#include<iostream>
#include<unordered_set>
using namespace std;

vector<string> findWords(vector<string>& words) {
	vector<string> res;
	unordered_set<char> row1{ 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' };
	unordered_set<char> row2{ 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' };
	unordered_set<char> row3{ 'z', 'x', 'c', 'v', 'b', 'n', 'm' };
	for (string word : words) {
		int one = 0, two = 0, three = 0;
		for (char c : word)
		{
			if (row1.count(c)) one = 1;
			if (row2.count(c)) two = 1;
			if (row3.count(c)) three = 1;
			if ((one + two + three)>1) break;
		}
		if ((one + two + three) == 1)  res.push_back(word);

	}

	return res;

}

int main(){
	vector<string>words;
	vector<string>new_words;
	words.push_back("Hello");
	words.push_back("Alaska");
	words.push_back("Dad");
	words.push_back("Peace");
	new_words=findWords(words);
	for (int i = 0; i < new_words.size();i++){
		cout << new_words[i]<<endl;
	}
	return 0;
}


这样的时候输出不了new_words

报错:

错误    1    error C2679: 二进制“<<”: 没有找到接受“std::basic_string<char,std::char_traits<char>,std::allocator<char>>”类型的右操作数的运算符(或没有可接受的转换)    F:\C++\argc\argc\arg.cpp    44    1    argc

后来加一个头文件#include<sstream>就好了,



查了下,应该是输出字符串和new_words[i]不符?:


<sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本。
<sstream>使用string对象来代替字符数组。这样可以避免缓冲区溢出的危险。而且,传入参数和目标对象的类型被自动推导出来,即使使用了不正确的格式化符也没有危险。
istringstream和ostringstream主要用在内核格式化中(用cout的ostream方法将格式化信息写入string对象中或是读取string对象中的格式化信息)例如:
ostringstream outstr;
double price= 281.00;
char* ps = "for a copy of the ISO/EIC C++ standard!";
outstr << fixed;
outstr << "Pay only$" << price << ps << end;
string msg = outstr.str();
istreamstring允许用istream方法读取istringsteam对象中的数据,也可以用使用string对象对istreamsting对象初始化。简而言之:istirngstream和ostringstream可以使用
istream和ostream类的方法来管理存储在字符串的字符数据。
stringstream通常是用来做数据转换的。相比c库的转换,它更加安全,自动和直接。
例如:#include <string>
#include <sstream>
#include <iostream>

int main()
{
    std::stringstream stream;
    std::string result;
    int i = 1000;
    stream << i; //将int输入流
    stream >> result; //从stream中抽取前面插入的int值
    std::cout << result << std::endl; // print the string "1000"
}

除了基本类型的转换,也支持char *的转换。

 

#include <sstream>
#include <iostream>

int main()
{
    std::stringstream stream;
    char result[8] ;
    stream << 8888; //向stream中插入8888
    stream >> result; //抽取stream中的值到result
    std::cout << result << std::endl; // 屏幕显示 "8888"
}

需要注意的是,下面的写法是不正确的:ifsream fs(Filename);
stringsteam buff;
buff << fs.rubf();//这句代码可以一次性把文件写入一个字符串中,然后将Outbuff.str()的值赋给一个string对象就可以。
buff << fs;这样写是错误的,看看下面的<<运算符的定义就知道了,它不接受这样的参数。
但可以这样写fs>>buf;这样写才正确。
cout << Outbuff << endl;
这样写,编译器可以通过编译,但是运行后是空值。改成这样的才行:cout << Outbuff.rubf()<< endl;
istringstream和ostringstream在文件流的用法和stringstream的用法类似,必须用rubf方法才可以看到内容。
rubf返回的一个stringbuf 对象的指针,str方法返回的是一个string对象,上面的rubf也可以换成str方法。
这三个类的str和rubf的类方法用法都一样。
不同的是str方法:有两个版本:
string str()const;//拷贝流缓冲到一个string对象中
void str(constr string& s);//通过流缓冲构造一个string对象。上面的rubf也可以写出Outbuff.rubuf()->str(),这样些效率更高些。

需要特别注意的是:要清空上面的类对象的内存,不能用clear方法,那只是设置了错误标志位,要用str("");
stringstream的<<方法和ostream的 <<方法一样。
而且stringstream只有<<运算符可以用。
ostream& operator<< (bool& val );
ostream& operator<< (short& val );
ostream& operator<< (unsigned short& val );
ostream& operator<< (int& val );
ostream& operator<< (unsigned int& val );
ostream& operator<< (long& val );
ostream& operator<< (unsigned long& val );
ostream& operator<< (float& val );
ostream& operator<< (double& val );
ostream& operator<< (long double& val );
ostream& operator<< (void*& val );
 
ostream& operator<< (streambuf* sb );
 
ostream& operator<< (ostream& ( *pf )(ostream&));
ostream& operator<< (ios& ( *pf )(ios&));
ostream& operator<< (ios_base& ( *pf )(ios_base&));
上面的都是它的成员函数,下面的则是全局函数
ostream& operator<< (ostream& out, char c );
ostream& operator<< (ostream& out, signed char c );
ostream& operator<< (ostream& out, unsigned char c );

ostream& operator<< (ostream& out, const char* s );
ostream& operator<< (ostream& out, const signed char* s );
ostream& operator<< (ostream& out, const unsigned char* s );

我们还可以利用stringstream来清楚文件内容。示例代码如下:
ofstream fs(FileName);
stringstream str;
str<<fs;
fs.close();
这样文件就被清空了,但是文件还在。




最终:

#include<stdio.h>
#include<vector>
#include<iostream>
#include<unordered_set>
#include<sstream>
using namespace std;

vector<string> findWords(vector<string>& words) {
	vector<string> res;
	unordered_set<char> row1{ 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' };
	unordered_set<char> row2{ 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' };
	unordered_set<char> row3{ 'z', 'x', 'c', 'v', 'b', 'n', 'm' };
	for (string word : words) {
		int one = 0, two = 0, three = 0;
		for (char c : word)
		{
			if (row1.count(c)) one = 1;
			if (row2.count(c)) two = 1;
			if (row3.count(c)) three = 1;
			if ((one + two + three)>1) break;
		}
		if ((one + two + three) == 1)  res.push_back(word);

	}

	return res;

}

int main(){
	vector<string>words;
	vector<string>new_words;
	words.push_back("Hello");
	words.push_back("Alaska");
	words.push_back("Dad");
	words.push_back("Peace");
	new_words=findWords(words);
	for (int i = 0; i < new_words.size();i++){
		cout << new_words[i]<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值