类功能的toString()C ++

问题描述 投票:0回答:1

我定义了一个名为与函数类应该返回设定为对应的字符串。我有了解如何实现这是我的老师也没做解释做一个好工作的重大问题。任何方向性的帮助或解释将非常感激。我已经发表我的教授想要的设置。

EDIT1:添加为清楚起见,我理解如何实现大部分的副手其它功能,但由于某种原因的toString()函数只是真的不跟我一下。另外的函数的名称给予我们专门用这种方式使联盟应该是资本,因为它与其他命令干扰。

#include <iostream>
#include <algorithm>
#include <set>
#include <iterator>


class Set
{
public:
    void add(int i);
    bool belongs(int i);
    void difference(Set B);
    void Union(Set B);
    void intersect(Set B);
    std::string toString();
};

int main()
{
    Set A;
    Set B;

    std::cout << "printing A" << std::endl;
    A.toString();
    std::cout << std::endl << "printing B" << std::endl;
    B.toString();
    std::cout << std::endl << "adding 12 to A" << std::endl;
    A.add(12);
    std::cout << std::endl << "printing A" << std::endl;
    A.toString();
    std::cout << std::endl << "does 4 belong to A" << std::endl;
    A.belongs(4);
    std::cout << std::endl << "does 11 belong to A" << std::endl;
    A.belongs(11);
    std::cout << std::endl << " remove B from A" << std::endl;
    A.difference(B);
    std::cout << std::endl << "printing A" << std::endl;
    A.toString();
    std::cout << std::endl << "union of A and B" << std::endl;
    A.Union(B);
    std::cout << std::endl << "printing A" << std::endl;
    A.toString();
    std::cout << std::endl << "intersecting A and B" << std::endl;
    A.intersect(B);
    std::cout << std::endl << "printing A" << std::endl;
    A.toString();
}

//add the number i to the set
void Set::add(int i)
{

}

//return true if i is a member of the set
bool Set::belongs(int i)
{

}

//removes B from the set A where A is the current set so A=A-B
void Set::difference(Set B)
{

}

//performs A U B where A is the current set and the result is stored in A
void Set::Union(Set B)
{

}

//performs A B where A is the current set and the result is stored in A
void Set::intersect(Set B)
{

}

//displays the set in roster notation {1, 2, 3} etc
std::string Set::toString()
{

}
c++ function set tostring
1个回答
2
投票

你的教授要你做的是写一个函数std::string Set::toString(){ ... }将返回一个包含你的对象的元素std::string内部容器(我怀疑可能是基于你的功能std::vector)将返回一个包含正确的格式元素的字符串。

您将需要考虑如何遍历内部容器和追加元素到字符串您正在使用string::append返回。但愿这是不够的方向真正开始在功能上并执行它,因为它是相当直截了当。你可能需要使用to_string()方法追加之前整数转换为字符串。

© www.soinside.com 2019 - 2024. All rights reserved.