I am trying to convert a string vector to a float vector ideally i would like to convert it to a float array as this would be easier for me to work with but from research i have come to understand that a vector is preferable to an array. This is the code i am still a beginner c++ coder and i only kinda of understand whats going on in the code lol. The program keeps giving me and error when i try to run it i don't understand why but i know it has something to do with my attempt at converting the string vector to a float.
#include <vector>
#include <string>
#include <iostream>
#include "sqlite3.h"
#include <cassert>
#include <sstream>
#include <iterator>
#include <algorithm>
using namespace std;
sqlite3* db;
using Records = vector<Record>;
int select_callback(void *p_data, int num_fields, char **p_fields, char **p_col_names)
{
Records* records = static_cast<Records*>(p_data);
try {
records->emplace_back(p_fields, p_fields + num_fields);
}
catch (...) {
// abort select on failure, don't let exception propogate thru sqlite3 call-stack
return 1;
}
return 0;
}
Records select_stmt(const char* stmt)
{
Records records;
char *errmsg;
int ret = sqlite3_exec(db, stmt, select_callback, &records, &errmsg);
if (ret != SQLITE_OK) {
std::cerr << "Error in select statement " << stmt << "[" << errmsg << "]\n";
}
else {
std::cerr << records.size() << " records returned.\n";
}
return records;
}
void sql_stmt(const char* stmt)
{
char *errmsg;
int ret = sqlite3_exec(db, stmt, 0, 0, &errmsg);
if (ret != SQLITE_OK) {
std::cerr << "Error in select statement " << stmt << "[" << errmsg << "]\n";
}
}
int main()
{
if (sqlite3_open("BandDia.db", &db) != SQLITE_OK) {
std::cerr << "Could not open database.\n";
return 1;
}
Records records = select_stmt("SELECT * FROM k_EnergyEig_T2 ");
sqlite3_close(db);
vector<float> v;
istringstream iss(Records);
std::copy(std::istream_iterator<float>(iss),
std::istream_iterator<float>(),
std::back_inserter(v));
// Put the result on standard out
std::copy(v.begin(), v.end(),
std::ostream_iterator<float>(std::cout, ", "));
std::cout << "\n";
/* for (auto& record : records) {
// do something with your records
puts("Hello,World");
}*/
return 0;
}