0
#include <cstdlib>
#include <algorithm>
#include <string>
#include <set>
#include <iostream>
#include <cstdio>
using namespace std;

void show(set<string> M)
{
    printf("\n \t Матрица А:\n ");
    for (set<string>::iterator ss=M.begin();ss!=M.end();++ss)
        cout<<*ss<<endl;
}

int main()
{
    system("chcp 1251>nul");
    string fam;
    set<string> A;
    A.insert(fam);
    cout<<"\n"<<"\t"<< "Ваша фамилия 1 - ";
    cin>>fam;
    A.insert(fam);
    cout<<"\t"<< "Ваша фамилия 2 - ";
    cin>>fam;
    A.insert(fam);
    cout<<"\t"<< "Ваша фамилия 3 - ";
    cin>>fam;
    A.insert(fam);
    cout<<"\t"<< "Ваша фамилия 4 - ";
    cin>>fam;
    A.insert(fam);
    cout<<"\t"<< "Ваша фамилия 5 - ";
    cin>>fam;
    A.insert(fam);
    cout<<"\t"<< "Ваша фамилия 6 - ";
    cin>>fam;
    A.insert(fam);
    cout<<"\t"<< "Ваша фамилия 7 - ";
    cin>>fam;
    A.insert(fam);
    cout<<"\t"<< "Ваша фамилия 8 - ";
    cin>>fam;
    A.insert(fam);
    cout<<"\t"<< "Ваша фамилия 9 - ";
    cin>>fam;
    A.insert(fam);
    cout<<"\t"<< "Ваша фамилия 10 - ";
    cin>>fam;
    A.insert(fam);
    cout<<"\t"<< "Ваша фамилия 11 - ";
    cin>>fam;
    cout<<"\t";
    show(A);
    system("pause");
    return 0;
}

Почему при вводе 11 фамилий выводится только 10 фамилий?

1 ответ 1

2

А зачем вы сначала добавляете строку в множество, а только потом вводите ее? Понятно, что первая строка пустая, а последняя - потеряна...

Так что - ни при вводе, ни при выводе, а при обработке :)

Я бы делал так:

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

using namespace std;

void show(const set<string>& M)
{
    cout << "\n \t Матрица А:\n ";
    for(set<string>::iterator ss = M.begin(); ss != M.end(); ++ss)
        cout<<*ss<<endl;
}

int main()
{
    // system("chcp 1251>nul");
    set<string> A;
    for(int i = 1; i <= 11; ++i)
    {
        string fam;
        cout<<"\n\tВаша фамилия " << i << " - ";
        cin>>fam;
        A.insert(fam);
    }
    show(A);
    system("pause");
    return 0;
}

Но учтите, что set не допускает одинаковые значения, так что если введете одинаковые фамилии - из них выведена будет только одна.

2
  • т.е. СТРОКА A.insert(fam); после set<string> A; лишняя
    – igorekvp
    28 фев 2018 в 16:27
  • 1
    А после последнего cin>>fam ее не хватает.
    – Harry
    28 фев 2018 в 16:58

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.