C++ C++;它实现了流

C++ C++;它实现了流,c++,stream,C++,Stream,我想编写一个包含两个函数的类映射:save和load。 我想使用流,这样我就可以在我的程序中编写: 映射这里是如何重载运算符的简单示例 类映射 { 映射和运算符>(标准::字符串映射名) { //保存地图 return*this;//这使您能够保存多个映射 //在一行中重复了几次! } }; //用法 地图m1; m1>“保存的地图名称”//保存地图 地图m2; m2“save-map1”>>“save-map2”//保存到两个不同的名称! 根据使用情况,可能不希望将两个或多个贴图合并到一个对

我想编写一个包含两个函数的类映射:save和load。 我想使用流,这样我就可以在我的程序中编写:
映射这里是如何重载
运算符的简单示例

类映射
{
映射和运算符>(标准::字符串映射名)
{
//保存地图
return*this;//这使您能够保存多个映射
//在一行中重复了几次!
}
};
//用法
地图m1;
m1>“保存的地图名称”//保存地图
地图m2;
m2“save-map1”>>“save-map2”//保存到两个不同的名称!
根据使用情况,可能不希望将两个或多个贴图合并到一个对象中。如果是这样,那么您可以使操作符的返回类型重载
操作符,并将它们声明为类的
朋友,然后使用它们。下面是一个示例代码

#include <iostream>
#include <string>
using namespace std;
class Map
{
   friend Map& operator << (Map &map, string str);
   friend Map& operator >> (Map &map, string str);
};

Map& operator << (Map &map, string str)
{
  //do work, save the map with name str
  cout << "Saving into \""<< str << "\"" << endl;

  return map;
}

Map& operator >> (Map &map, string str)
{
  // do work, load the map named str into map
  cout << "Loading from \"" << str << "\"" << endl;

  return map;
}

int main (void)
{
  Map map;
  string str;

  map << "name1";
  map >> "name2";
}
#包括
#包括
使用名称空间std;
类图
{
好友映射和操作符>(映射和映射,字符串str);
};
地图和操作员“hi”
可以表示将
obj
保存在两个名为“hello”和“hi”的文件中

#include <iostream>
#include <string>
using namespace std;
class Map
{
   friend Map& operator << (Map &map, string str);
   friend Map& operator >> (Map &map, string str);
};

Map& operator << (Map &map, string str)
{
  //do work, save the map with name str
  cout << "Saving into \""<< str << "\"" << endl;

  return map;
}

Map& operator >> (Map &map, string str)
{
  // do work, load the map named str into map
  cout << "Loading from \"" << str << "\"" << endl;

  return map;
}

int main (void)
{
  Map map;
  string str;

  map << "name1";
  map >> "name2";
}