【问题标题】:In C++ how do I store an SQL query and sort it?在 C++ 中,我如何存储 SQL 查询并对其进行排序?
【发布时间】:2016-03-24 00:17:25
【问题描述】:

我有一个葡萄酒数据库,我正在尝试从中检索数据,以存储在向量中并对结构进行排序。我似乎无法理解如何在 C++ 中存储一个 sql 查询,我有一个选择整个表的命令和一个处理行的 while 循环,但是世界上如何保存查询的结果。更具体地说,我如何将查询存储在要排序的向量中?

我是不是想多了?我一直在思考和测试不同的 几个小时,结合这两种语言的逻辑让我很生气。

我可以对整行进行排序吗?在我的代码中,我可以对row[2] 进行排序吗? 我可以将整行存储到向量中吗?

这是我的代码,它有一个 sql 命令来从数据库中获取所有葡萄酒信息。然后有一个while循环来显示所有内容。如何将 sql_cmd 中的所有内容存储在向量中?

如果您需要,我可以指定更多并提供更多代码 sn-ps, 让我知道我是否仍然一般。

【问题讨论】:

  • 你能发布代码而不是截图吗?
  • 为什么使用char* 而不是const char* 来指向您的常量数据?

标签: c++ mysql sorting vector


【解决方案1】:

你可以的

class Wine
{
  public:
    string name;
    string vintage;
    string price;    // Maybe you want to convert to double instead of storing a string
    // and so on
}

vector<Wine> myWines;

在您的 while 循环中,您可以添加:

Wine tmp;
tmp.name = row[0];
tmp.vintage = row[1];
// and so on
myWines.push_back(tmp);

现在您可以像这样按名称排序

std::sort(myWines.begin(), myWines.end(),
      [] (Wine const& a, Wine const& b) { return a.name < b.name; });

或者像这样的年份

std::sort(myWines.begin(), myWines.end(),
      [] (Wine const& a, Wine const& b) { return a.vintage < b.vintage; });

排序可以展示如下:

class Wine
{
public:
    string name;
    string vintage;
    string price;    // Maybe you want to convert to double instead of storing a string
    // and so on
};

int main()
{
    vector<Wine> myWines;

    Wine tmp;
    tmp.name = "d";
    tmp.vintage = "3";
    tmp.price = "i";
    myWines.push_back(tmp);

    tmp.name = "g";
    tmp.vintage = "1";
    tmp.price = "f";
    myWines.push_back(tmp);

    tmp.name = "a";
    tmp.vintage = "2";
    tmp.price = "c";
    myWines.push_back(tmp);

    cout << "Unsorted" << endl;

    // Print the vector
    for (auto& v : myWines)
    {
        cout << v.name << " " << v.vintage << " " << v.price << endl;
    }

    cout << "Sort by name" << endl;
    std::sort(myWines.begin(), myWines.end(),
              [] (Wine const& a, Wine const& b) { return a.name < b.name; });

    // Print the vector
    for (auto& v : myWines)
    {
        cout << v.name << " " << v.vintage << " " << v.price << endl;
    }

    cout << "Sort by vintage" << endl;
    std::sort(myWines.begin(), myWines.end(),
          [] (Wine const& a, Wine const& b) { return a.vintage < b.vintage; });

    // Print the vector
    for (auto& v : myWines)
    {
        cout << v.name << " " << v.vintage << " " << v.price << endl;
    }

    return 0;
}

输出:

Unsorted
d 3 i
g 1 f
a 2 c
Sort by name
a 2 c
d 3 i
g 1 f
Sort by vintage
g 1 f
a 2 c
d 3 i

【讨论】:

  • 啊..我没想过创建临时值作为向量的占位符。厉害了,赞一个!泰!
  • @BrianDelaCruz - 好吧,你真的不需要制作临时变量。我这样做是为了保持简单。如果您看到 Paul Evans 的答案,您可以了解如何避免使用 temp 变量。在 Pauls 示例中,Wine 的实例在被推送到向量时在同一行中创建。
【解决方案2】:

您想要一个struct 来存储您的数据,例如:

struct Wine {
    Wine(const string& name,
         const string& vintage,
         const string& rating,
         float price,
         const string& type) : name_(name), vintage_(vintage), rating_(rating), price_(price), type_(type) {};
    string name_;
    string vintage_;
    string rating_;
    float price_;
    string type_;
}

vector<Wine> vec;

然后在你的循环中:

vec.push_back(Wine(row[0],row[1],row[2],row[3],row[4]));

【讨论】:

  • 这个和教授提供的例子差不多,感谢有识之士。
猜你喜欢
  • 2018-05-02
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
  • 1970-01-01
  • 2021-05-20
  • 1970-01-01
相关资源
最近更新 更多