我有一个电话簿程序,我想通过输入参数来改进它
从键盘读取的字符串 vector 中。

我尝试了这段代码,但是在 vector 中无法识别参数;

string firstname, lastname, country, city, street;
string phone;
vector<string> user( firstname, lastname, country, city, street, phone);

最佳答案

您可以像这样使用初始化程序列表构造函数(https://en.cppreference.com/w/cpp/container/vector/vector#7):

#include <string>
#include <vector>

using std::string;
using std::vector;

int main()
{
  string firstname, lastname, country, city, street;
  string phone;
  vector<string> user{ firstname, lastname, country, city, street, phone };

  return 0;
}

关于c++ - 字符串 vector (从键盘读取),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56145034/

10-11 22:13
查看更多