结构中的vector <string>无法正常工作

时间:2019-11-15 14:52:53

标签: c++ string vector struct

我声明了vector<string>,甚至无法编译它。我尝试了许多方法,但没有一个起作用。 我正在尝试写出x.surname.push_back(word)[i],但它的定义是错误的,我也不知道如何正确编写它并使之编译。

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int number, i = 0;
  string word;
  struct donators {
    vector<string> surname;
    vector<int> amount;
  } x;

  cout << "How many donators do you want to register? " << endl;
  cin >> number;

  for (i = 0; i < number; i++) {
    cout << "Surname: ";
    cin >> word;
    x.surname.push_back(word)[i];

    cout << "Amount: ";
    x.amount.push_back(i);
    cin >> x.amount[i];
  }
  cout << "OUR GORGEUS DONATORS: " << endl;
  for (i = 0; i < number; i++) {

    if (x.amount[i] >= 10000) {
      cout << "Surname: " << x.surname(word)[i];
      cout << "Amount: " << x.amount[i] << endl;
    }

    else if (x.amount[i] < 10000) {
      cout << "Lack of surnames!" << endl;
    }
  }
  cout << "OUR CASUAL DONATORS: " << endl;

  for (i = 0; i < number; i++) {

    if (x.amount[i] < 10000) {
      cout << "Surname: " << x.surname(word)[i];
      cout << "Amount: " << x.amount[i] << endl;
    } else if (x.amount[i] >= 10000) {
      cout << "Lack of surnames!" << endl;
    }
  }

  return 0;
}

还有一件事。如何使句子“缺少姓氏!”要写一次?在某些情况下,它会被重复两次或多次重复。

2 个答案:

答案 0 :(得分:0)

您正在将[i]放在代码中看似随机的位置。例如x.surname.push_back(word)[i];中。如果不确定它们的功能,请不要在代码中添加类似的内容。

x.surname(word)[i]构造也是错误的。 x.surname(word)应该是什么?此语法用于函数调用。 surname不是函数。这是std::vector<std::string>。只需放x.surname[i]即可。

  

还有一件事。如何使句子“缺少姓氏!”成为   写一次?在某些情况下,它被写入两次或多次   什么是多余的。

那是因为您为每个不符合标准的捐助者编写了它。相反,请跟踪是否有任何捐助者符合标准,并且仅在没有最终符合条件时才打印。您可以这样做:

bool HasGorgeousDonators = false;

然后在循环中:

if (x.amount[i] >= 10000)
{
    cout << "Surname: " << x.surname[i];
    cout << "Amount: " << x.amount[i] << endl;
    HasGorgeousDonators = true;
}

在循环之后:

if (!HasGorgeousDonators)
    cout << "Lack of surnames!" << endl;

与其他循环类似。另外,请考虑以下问答:

Why is "using namespace std;" considered bad practice?

答案 1 :(得分:0)

似乎您正在使用某些C ++帮助功能编写C。但是C ++是另一种语言。当然,它支持某些C结构,但还有更多。 看看我的一些实施建议,并将其与您的代码进行比较:

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

template<typename T>
T ReadCin(std::string_view const& sv = "") {
    T retVal;
    if (!sv.empty()) std::cout << sv;
    std::cin >> retVal;
    return retVal;
}

class Donator {
private:
    std::string surname{};
    int amount{};
public:
    constexpr bool IsGenerous() const noexcept { return amount >= 10000; }
    void Read() noexcept {
        surname = ReadCin<decltype(surname)>("Surname: ");
        amount = ReadCin<decltype(amount)>("Amount: ");
    }
    friend std::ostream& operator<<(std::ostream& out, Donator const& donator) noexcept {
        out << "Surname: " << donator.surname << ", " << "Amount: " << donator.amount;
        return out;
    }
};

int main() {
    std::vector<Donator> donators(ReadCin<int>("How many donators do you want to register?\n"));
    for (auto& donator : donators) donator.Read();

    std::cout << "OUR GENEROUS DONATORS:\n";
    std::copy_if(std::cbegin(donators), std::cend(donators), std::ostream_iterator<Donator>(std::cout, "\n"),
        [](Donator const& donator) { return donator.IsGenerous(); });

    std::cout << "OUR CASUAL DONATORS:\n";
    for (auto const& donator : donators) if (!donator.IsGenerous()) std::cout << donator << '\n'; //alternative
}

我尝试使用C ++包括一些可能性。我真的建议您获得有关C ++的好书。