作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 T-SQL 中有一个查询,它返回 300 条记录。每条记录有 2 列 (date, int)
在 C++ 中,将所有日期放入一个 vector 而将所有整数放入另一个 vector 的最简单方法是什么?
我想在一个函数中完成它。
最佳答案
在不了解您的 SQL 客户端库的情况下很难提供完整的代码 - 这会影响您填充 vector
的方式s,但基本上你循环遍历从数据库中读取的行 push_back
在你的两个vector
s 表示每行中检索到的值。
主要问题是您将如何处理返回的参数?你有两个 vector
s,因为您已在此处指定问题。您可以通过让调用者创建 vector
来实现此目的s 然后函数填充它们,如下所示:
#include <vector>
// function declaration - return false on error, or throw exception if preferred
bool populate(std::vector<double>& dates, std::vector<int>& values);
// calling code
std::vector<double> myDates;
std::vector<int> myValues;
// if you know the row count is 300 ahead of time, do this
unsigned int rowCount;
// rowCount gets set up, to 300 in this example
myDates.reserve(rowCount);
myValues.reserve(rowCount);
// Populate vectors, checking for error (false = error)
if (populate(myDates, myValues)) {
// work with the returned data
}
为了获得更好的封装或行数据带来的额外功劳,我倾向于使用 POD 结构的 vector 。这样做的好处是每个日期和值都保持紧密耦合 - 如果您希望对每一行执行操作,您可以将其扩展为一个完整的类。最好将数据隐藏在 getter 后面。
struct Row {
public:
double date;
int value;
};
bool populate(std::vector<Row>& rows);
关于c++ - 在 C++ 中将 SQL 查询返回到 vector<double> 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6611157/
到目前为止,我已经生成了以下代码来尝试将相关数据整合在一起。 但是,使用“+ 7”函数会产生以下问题。 Registration date = '2018-01-01' 它正在推迟 2018-04-0
我已经成功地将我的自定义购物车发布到 PayPal——它处理订单非常漂亮,当收到付款时,它会将数据发回我在配置中指定的 URL。代码基于此处找到的库:http://www.phpfour.com/bl
我是一名优秀的程序员,十分优秀!