我正在尝试将列表添加到字符串中。
int main() {
std::cout << "Hello, welcome to Jay's Coffee!!n";
std::string name; std::cout <<"What is your name "; std::cin >> name;
std::cout <<"Hello " << name << ", thank you so much for coming in today";
std::list <std::string> menu = {"Black Coffee" "Espresso" "Latte" "Cappucino"};
std::cout << name <<",what would you like from our menu today? Here is what we are serving.n" << menu;
}
返回
invalid operands to binary expression ('basic_ostream<char>' and 'std::list<std::string>' (aka 'list<basic_string<char>>'))
列表没有operator<<
。你必须写一个循环。例如
for (auto& item : menu)
{
std::cout << item << 'n';
}
如果你仔细想想,很明显为什么你必须自己做这件事。你是如何将列表项目分开的?我已经选择把每一项都放在一行。你可以选择用逗号或空格或一些奇特的格式来分隔它们。因为没有明显的单一方式来打印列表,所以在C++库中没有预定义的方式。
您应该用这种方式编写代码。在c++中,不能直接打印列表。
#include <string>
#include <list>
using namespace std;
int main() {
cout << "Hello, welcome to Jay's Coffee!!n";
string name;
cout <<"What is your name ";
cin >> name;
cout <<"Hello " << name << ", thank you so much for coming in today";
list <string> menu = {"Black Coffee", "Espresso", "Latte", "Cappucino"};
cout << name <<",what would you like from our menu today? Here is what we are serving.n" ;
for ( string& s : menu )
{
cout << s << 'n';
}
}
错误消息表示运算符<lt;没有为类std::list<std::string>
定义要与类型为std::list<std::string>
的对象menu
一起使用的。
此外,您还需要用逗号分隔初始值设定项列表中的字符串。
std::list <std::string> menu = {"Black Coffee", "Espresso", "Latte", "Cappucino"};
否则,由于字符串文字的串联,列表将只包含一个字符串。
您可以定义这样一个运算符,如下面的演示程序所示。
#include <iostream>
#include <string>
#include <list>
std::ostream & operator <<( std::ostream &os, const std::list<std::string>& lst )
{
for ( const auto &s : lst )
{
os << s << 'n';
}
return os;
}
int main()
{
std::list <std::string> menu =
{
"Black Coffee", "Espresso", "Latte", "Cappucino"
};
std::cout << menu;
}
程序输出为
Black Coffee
Espresso
Latte
Cappucino
或者像一样直接在主菜单中使用基于范围的循环
std::cout << name <<",what would you like from our menu today? Here is what we are serving.n";
for ( const auto &s : menu )
{
std::cout << s << 'n';
}
或者将基于范围的for循环放置在类似于运算符<lt;如上所示。
首先,您没有定义列表:在菜单的声明中,初始化器列表元素没有用逗号(,
(分隔。
为了使其可重复使用,我会这样做:
#include <iostream>
#include <list>
static const std::string list_sep = ", ";
template<typename C> struct FormattedContainer
{
FormattedContainer(const C& cont, const std::string& sep = list_sep)
: cont_(cont)
, sep_(sep) {}
friend std::ostream& operator<<(std::ostream& os, const FormattedContainer& fc)
{
bool first = true;
for (auto&& e : fc.cont_)
{
if (first)
{
os << e;
first = false;
}
else
{
os << fc.sep_ << e;
}
}
return os;
}
const C& cont_;
const std::string sep_;
};
template<typename C>
auto make_fc(const C& cont, const std::string& sep = list_sep)
-> FormattedContainer<C>
{
return FormattedContainer<C>(cont, sep);
}
int main() {
std::list <std::string> menu = {"Black Coffee", "Espresso", "Latte", "Cappucino"};
std::cout << "What would you like from our menu today? Here is what we are serving.n" << make_fc(menu);
}
这样,你就不需要为std
命名空间中的某个东西定义operator<<
(这可能会导致不明确的调用,因为其他人也可能会定义它(,不需要导入另一个命名空间,只需围绕类型调用一个包装器即可。使用此方法,你基本上可以将它与任何容器或可迭代类型一起使用。
所有其他答案都是正确的,但这里是进行相同的更好方法
#include <iostream>
#include <algorithm>
#include <list>
template <typename T>
std::ostream & operator << (std::ostream & os, const std::list<T> & vec){
std::for_each (vec.begin () , vec.end() , [&](const auto& val){
std::cout << val << " ";
});
return os;
}
int main () {
std::list <std::string> menu = {"Black Coffee", "Espresso", "Latte", "Cappucino"};
std::cout << menu << "n";
return 0;
}