C++ 类它实现了流

发布于 2024-11-09 03:35:27 字数 252 浏览 0 评论 0原文

我想编写一个具有两个功能的类 Map:保存和加载。 我想使用流,这样我就可以在我的程序中编写: 地图<< “地图名称”,它会将地图加载到内存中,并地图>> “地图名称”,它会保存我的地图。

不幸的是,在谷歌中我只能找到如何覆盖运算符“>>” '<<',但在运算符左侧使用 cout 或 cin。

你能给我同样的提示吗? 感谢您提前答复。

I want to write a class Map with two functions: save and load.
I'd like to use streams so I could write in my program:
map << "map name" and it'd load a map to memory and map >> "map name" and it'd save my map.

Unfortunately in google I can only find how to override the operators '>>' '<<',but using cout or cin at the left side of the operator.

Can You give me same hints how to do it ?
Thanks for answer in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苄①跕圉湢 2024-11-16 03:35:27

重载 <<>> 运算符并将它们声明为类的 friend,然后使用它们。这是示例代码。

#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";
}

请注意,根据您的目的,对对象返回的解释取决于您,因为 obj << “你好”<< "hi"; 可以意味着从 "hello" 和 "hi" 加载 obj 吗?或按该顺序附加它们,这取决于您。还有obj >>> “你好”>> "hi"; 可以表示将 obj 保存在名为“hello”和“hi”的两个文件中

overload the << and >> operators and declare them as friend to your class, and then use them. Here is a sample code.

#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";
}

Note that in your purpose there interpretation of the returning of the object is upto you because obj << "hello" << "hi"; can mean load the obj from both "hello" and "hi" ? or append them in that order, it is upto you. Also obj >> "hello" >> "hi"; can mean save the obj in two files named "hello" and "hi"

陌上青苔 2024-11-16 03:35:27

以下是如何重载 operator<<operator>> 的简单说明,

class Map
{

   Map & operator<< (std::string mapName)
   {
       //load the map from whatever location
       //if you want to load from some file, 
       //then you have to use std::ifstream here to read the file!

       return *this; //this enables you to load map from 
                     //multiple mapNames in single line, if you so desire!
   }
   Map & operator >> (std::string mapName)
   {
       //save the map

       return *this; //this enables you to save map multiple 
                     //times in a single line!
   }
};

//Usage
 Map m1;
 m1 << "map-name" ; //load the map
 m1 >> "saved-map-name" ; //save the map

 Map m2;
 m2 << "map1" << "map2"; //load both maps!
 m2 >> "save-map1" >> "save-map2"; //save to two different names!

具体取决于用例,可能不需要将两个或多个映射放入单个对象。如果是这样,那么您可以将运算符<<的返回类型设置为void

Here is simple illustration how you can overload operator<< and operator>>

class Map
{

   Map & operator<< (std::string mapName)
   {
       //load the map from whatever location
       //if you want to load from some file, 
       //then you have to use std::ifstream here to read the file!

       return *this; //this enables you to load map from 
                     //multiple mapNames in single line, if you so desire!
   }
   Map & operator >> (std::string mapName)
   {
       //save the map

       return *this; //this enables you to save map multiple 
                     //times in a single line!
   }
};

//Usage
 Map m1;
 m1 << "map-name" ; //load the map
 m1 >> "saved-map-name" ; //save the map

 Map m2;
 m2 << "map1" << "map2"; //load both maps!
 m2 >> "save-map1" >> "save-map2"; //save to two different names!

Depending on the use-cases, it may not be desirable to two or more maps, into a single object. If that is so, then you can make the return type of the operator<< void.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文