0

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;
}
  • Could you give an example of what you're trying to do? What strings are in the input vector, and what numbers do you want in the resulting vector (or array)? – Beta Mar 31 '17 at 00:32
  • The program is taking in data from an sql table and passing it to the a vector to be used in the main function only problem is its a string type but i want it to be a float type, hence why i am trying to convert it – Audio Visual Mar 31 '17 at 02:11

1 Answers1

0

If I understand the question correctly, you have a string which contains comma separated values and you want to convert this into an array of floats.

The first thing that I would do is split the string into substrings. Each substring will contain one value. Examples of this can be found here.

The next thing to do is to iterate over these strings and convert them into floats. For this you can use the atof function. atof stands for 'a' (string) to 'f' (float). Note that you can easily convert a std::string into a c string using s.c_str().

Finally, it's probably better to use vectors here because they can be resized dynamically. If you really want to convert the output to an array, you can always do this:

float* a = new float[v.size()];
memcpy(a, &v[0], sizeof(float) * v.size());
Community
  • 1
  • 1
Jay
  • 636
  • 6
  • 19