谁能帮我解决这个问题?

问题描述 投票:-3回答:1

请说明以下函数调用的效果?我们需要准备以下函数调用。

   set<string> s;
    s.insert("hello");
    s.insert("bye");
    set<string> u(s);
    s.insert(u.begin(), u.end());
    set<string> t;
    t.insert("123");
    s.insert(t.begin(), t.end());
    cout << t << endl;
    cout << (t - s) << endl;
    cout << (s - t) << endl;
    cout << boolalpha;

    cout << contains(s, string("ace")) << endl;
    cout << contains(s, string("123")) << endl;
    s = s * t;
    cout << s << endl;
    t = t * s;
    cout << t << endl;
c++ string cout effect function-call
1个回答
0
投票

我创建了一个带有注释的工作示例,以帮助您了解它的作用。请在最后回答问题,以便完成。

#include <algorithm>
#include <iostream>
#include <set>
#include <string>

using namespace std;

int main()
{
    /*
        set can contain only unique elements

        that means if you insert the same string twice,
        only one occurence will remain in the set

        set is ordered, "a" is before "b"
    */

    // create an empty set
    set<string> s;

    // insert two strings into the set
    s.insert("hello");
    s.insert("bye");

    // create new set with the same elements as the old set
    set<string> u(s);

    /* insert range of multiple elements from set U into set S

        range starts at beginning of the set U
        because sets are ordered, the first string will be "bye"

        range ends at the end of the set U
        that means all elements from U were inserted into S

        before the operation, situation is as follows:
        S = { "hello", "bye" }
        U = { "hello", "bye" }

        because U was created from S,
        both sets contain the same elements
        and because sets cannot contain duplicates,
        no elements will be actually inserted
    */
    s.insert(u.begin(), u.end());

    // create empty set and insert one element into it
    set<string> t;
    t.insert("123");

    /*
        at this point situation is as follows:
        S = { "bye", "hello" }
        U = { "bye", "hello" }
        T = { "123" }

        Inserting whole set T to set S will insert only one element,
        that is "123"
    */
    s.insert(t.begin(), t.end());

    /*
        in your code you tried to display elements from the set
        using cout like this:

        cout << t << endl;

        this doesn't work because cout does not know how to display them
        at least not in C++17

        I created a for-each loop
        that will display each element from set T
        with newline between each two elements

        plus newline after all of them
    */
    cout << "T contains:\n";
    for(string e: t)
        cout << e << '\n';
    cout << '\n';

    // create a temporary set to store result of the difference operation
    set<string> diff;

    /*
        in your code you tried this:

        cout << (t - s) << endl;

        operator minus is not defined for sets, at least not in C++17
        but there is an operation called set_difference

        with arguments:
            start of the first set
            end of the first set
            start of the second set
            end of the second set
            information about storing the result
                in this case we will insert the elements to the DIFF set
                with std::inserter
    */
    set_difference(
        t.begin(), t.end(),
        s.begin(), s.end(),
        inserter(diff, diff.begin())
    );

    // display the result
    cout << "(t - s):\n";

    /*
        to understand the result we should look at the sets:
        S = { "bye", "hello", "123" }
        T = { "123" }

        (T - S) displays only elements that are in T but are not in S
        no such element exists
        that's why nothing is displayed
    */
    for(string e: diff)
        cout << e << '\n';
    cout << '\n';

    // delete all elements from DIFF so we can use it again
    diff.clear();

    /*
        S = { "bye", "hello", "123" }
        T = { "123" }

        the other way around, S - T
        elements "bye" and "hello" are in S but not in T,
        they will be displayed as the result
    */
    set_difference(
        s.begin(), s.end(),
        t.begin(), t.end(),
        std::inserter(diff, diff.begin())
    );

    // display result
    cout << "(s - t):\n";
    for(string e: diff)
        cout << e << '\n';
    cout << '\n';

    // set cout to display bools as text
    cout << boolalpha;

    /*
        in your code you tried:
        contains(s, string("ace"));

        again, I don't have this function here but it can be replaced with find

        find will try to find the requested element
        and return an iterator that points to the element
        or to the end of set if the element was not found

        so result of comparison between the returned iterator
        and end of set will be a bool

        true for element found
        false otherwise
    */
    cout << "\"ace\" is in set S: " << (s.find("ace") != s.end()) << '\n';
    cout << "\"123\" is in set S: " << (s.find("123") != s.end())  << '\n';

    return 0;
}

我不知道在这种情况下,运算符*应该做什么。您能给这个手术起个名字吗?谢谢。

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