I am new to BlackBerry Devevelopment. How can i convert a QtSoapArray to QStringList.
I have done this like,
Here m_ListEmails is QtSoapArray and m_Emails1 is QStringList
for (int var = 0; var < m_Emails1.count(); ++var) {
m_ListEmails.append(m_Emails1[var].toString());
qDebug() << "Email: "+ m_Emails1[var].toString();
}
Related
Write a assign_obj.h file so that the assign_driver file will output the
following. When implementing assign_obj.h think about how efficient your program will run. You must
implement your assign_obj class using a dynamic array.
assign_obj:: assign_obj() function will just set size to zero
assign_obj:: assign_obj(std::string s) function will go through each character of string and convert it to uppercase and push it the last of A
std::ostream& operator<<(std::ostream & out, assign_obj & obj) function will go through each data in obj.A vector and convert integer into string and
than output the data in this format [value:count value:count ....]
class assign_obj{
private:
struct item{
char value;
int count;
};
item *A;
int size;
public:
assign_obj();
assign_obj(std::string);
friend std::ostream& operator<<(std::ostream & out, assign_obj & obj);
}
int main(){
assign_obj ao1("dsdfdf");
std::cout << ao1 << std::endl;
}
This is the output that I want
[ D:1 S:1 D:1 F:1 D:1 F:1 ]
assign_obj:: assign_obj(){
size = 0;
}
assign_obj:: assign_obj(std::string s){
size = s.size();
for(int i = 0; i < s.size(); i++){
char c = std::toupper(s[i]);
A = new item{c,1};
}
}
# std::ostream& operator<<(std::ostream & out, assign_obj & obj){
std::cout<< "size: " << obj.size <<std::endl;
out << "[ ";
for(int i = 0; i < obj.size; i++){
out << obj.A[i].value << ":" << std::to_string(obj.A[i].count) << " ";
}
out << "]" << "\n";
return out;
}
I get a int32 : -1407942911 that I need to convert to a ip
I used this function:
_setIp(int _ip){
var _strData = StringBuffer();
for (int i = 0; i<4; i++){
_strData.write(_ip &0xff );
if (i < 3) {
_strData.write(".");
}
_ip = _ip >> 8;
}
return _strData.toString();
}
but I get the ip backwards: 1.127.20.172 instead of 172.20.127.1
I was trying to replicate this java code
public String longToIp(long ip) {
StringBuilder result = new StringBuilder(15);
for (int i = 0; i < 4; i++) {
result.insert(0,Long.toString(ip & 0xff));
if (i < 3) {
sb.insert(0,'.');
}
ip = ip >> 8;
}
return result.toString();
}
Update
I solved the error
static setIp(int _ip){
var _strData = StringBuffer();
for (int i = 0; i<4; i++){
_strData.write(_ip >> 24 &0xff );
if (i < 3) {
_strData.write(".");
}
_ip = _ip << 8;
}
return _strData.toString();
}
You can do something like this:
import 'dart:io';
import 'dart:typed_data';
void main() {
print(getIpFromInt32Value(-1407942911)); // 172.20.127.1
}
String getIpFromInt32Value(int value) => InternetAddress.fromRawAddress(
(ByteData(4)..setInt32(0, value)).buffer.asUint8List())
.address;
This solved the error:
static setIp(int _ip){
var _strData = StringBuffer();
for (int i = 0; i<4; i++){
_strData.write(_ip >> 24 &0xff );
if (i < 3) {
_strData.write(".");
}
_ip = _ip << 8;
}
return _strData.toString();
}
I would like to use prepared statements to insert thousands of rows at once into my postgres database. The data to insert is stored in a vector of structs.
By reading the answers to How to prepare statements and bind parameters in Postgresql for C++ I thought I found the way how to do it.
Unfortunately, the current version of libpqxx I am using doesn't support pqxx::prepare::invocation anymore and I wasn't able to find any alternative in the docs/internet.
The code I tried out for my purpose is the following:
//pqxx header
#include "pqxx/connection.hxx"
#include "pqxx/transaction.hxx"
#include "pqxx/nontransaction.hxx"
#include "pqxx/compiler-public.hxx"
#include "pqxx/result.hxx"
#include "pqxx/prepared_statement.hxx"
#include "pqxx/transaction_base.hxx"
#include "pqxx/internal/statement_parameters.hxx"
...
int main(){
struct testData {
string uuid;
float rndNo;
string timestamp;
};
vector<testData> dataBuffer;
testData dbdata;
string queryStr;
const char* query;
stringstream connStream;
stringstream queryStream;
string DB_NAME = "dbname";
string DB_USER = "postgres";
string DB_PASSWORD = "password";
string DB_HOSTADDR = "127.0.0.1";
string DB_PORT = "1234";
string DB_SCHEMA = "test_schema";
string tableName = "test_table";
//Create Random data
for (int i = 0; i < 100000; i++) {
dbdata.uuid = "fe2b22ba-6ce7-4bbc-8226-d7696fe7a047";
dbdata.rndNo = i / 3.123;
dbdata.timestamp = systemDateTimeSQLFormat(0);
dataBuffer.push_back(dbdata);
}
stringstream connStream;
connStream << "dbname = " << DB_NAME << " user = " << DB_USER << " password = " << DB_PASSWORD << " hostaddr = " << DB_HOSTADDR << " port = " << DB_PORT;
connection dbConn(connStream.str());
if (dbConn.is_open()) {
cout << "Opened database successfully: " << dbConn.dbname() << endl;
}
else {
cout << "Can't open database" << endl;
return;
}
pqxx::nontransaction W(dbConn);
std::string m_insertCommand = "INSERT INTO test_schema.test_table (column1, column2, column3) VALUES";
int buffSize = dataBuffer.size();
for (size_t i = 0; i < buffSize; i++)
{
unsigned int countOf$ = i * 3;
for (unsigned int i = 0; i < 3; ++i)
{
if (i == 0)
{
m_insertCommand += "(";
}
else
{
m_insertCommand += ", ";
}
m_insertCommand += "$";
std::stringstream ss;
ss << countOf$ + i + 1;
m_insertCommand += ss.str();
}
if (i < buffSize - 1)
m_insertCommand += ") ,";
}
m_insertCommand += ")";
//FOLLOWING CODE NOT POSSIBLE WITH libpqxx v12
dbConn.prepare("insert_into_db", m_insertCommand);
pqxx::prepare::invocation = W.exec_prepared("insert_into_db"); //prepare doesn't have a member "invocation"
for (size_t i = 0; i < buffSize; i++)
{
inv(dataBuffer.at(i).rndNo)(dataBuffer.at(i).timestamp)(dataBuffer.at(i).uuid); //inv not defined
}
inv.exec();
return 1;
}
pqxx::prepare::make_dynamic_params will probably solve your problem. It's solved my problem. Use this way:
for (size_t i = 0; i < buffSize; ++i)
{
auto element = dataBuffer.at(i);
vector<string> vect;
vect.reserve(3);
vect.push_back(pqxx::to_string(element.rndNo));
vect.push_back(element.timestamp);
vect.push_back(element.uuid);
work.exec_params(m_insertCommand, pqxx::prepare::make_dynamic_params(vect));
}
From the version 7.6.0 dynamic_params are deprecated. params can be used instead. Here is the new solution:
for (size_t i = 0; i < buffSize; ++i)
{
auto element = dataBuffer.at(i);
pqxx::params;
params.reserve(4);
params.append(pqxx::to_string(element.rndNo));
params.append(element.timestamp);
params.append(element.uuid);
params.append(); // For example insert null variable
work.exec_params(m_insertCommand, params);
}
The array ampVal has 25600 integers inside it. I need to find the maximum value of each set of 1024 values in the array and store it in another array. However I'm not getting this part to work it only gives 21 values and a random number '1348410436'. ampVal is a dynamic array.
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
int main() {
ifstream miniProject;
int n = 0;
miniProject.open("C:\\Users\\Simeon Ramjit\\Desktop\\audioframes.txt");
if (!miniProject) {
cout << "File not found" << endl;
}
else {
cout<<"File Located ! :D \nCounting Lines in file..." << endl;
while (miniProject) {
string lines;
getline(miniProject, lines);
n++;
}
cout << "Number of lines in file are: " << n << endl;
miniProject.close();
}
int *frNum = new int[n];
int *bitNum = new int[n];
int *ampVal = new int[n];
for (int i = 0; i < n;i++){
frNum[i] = 0;
bitNum[i] = 0;
ampVal[i] = 0;
}
miniProject.open("C:\\Users\\Simeon Ramjit\\Desktop\\audioframes.txt");
if (!miniProject) {
cout << "File not found" << endl;
}
else {
int i = 0;
string frameNumber, bitNumber, amplitudeValue;
while (miniProject) {
(miniProject >> frameNumber >> bitNumber >> amplitudeValue);
stringstream(frameNumber) >> frNum[i];
stringstream(bitNumber) >> bitNum[i];
stringstream(amplitudeValue) >> ampVal[i];
i++;
}
}
miniProject.close();
int frameGroupStart = 0;
int frameGroupEnd = 1024;
int maxAmpVal = 0;
while (frameGroupEnd != 25600) {
for (int i = frameGroupStart; i < frameGroupEnd; i++) {
if (ampVal[i] >maxAmpVal) {
maxAmpVal = ampVal[i];
cout << maxAmpVal << endl;
}
}
frameGroupStart = frameGroupStart + 1024;
frameGroupEnd = frameGroupEnd + 1024;
}
getchar();
return 0;
}
I used an alternate approach and this gets the job done:
int bitGroupStart = 0;
int bitGroupEnd = 1024;
int arrayOfMaxAmpVal[25];
int frameNumMaxVal[25];
int maxAmpVal = 0;
for (int i = 0; i < 25; i++) {
for (int j = bitGroupStart; j < bitGroupEnd; j++) {
if (ampVal[j] > maxAmpVal) {
maxAmpVal = ampVal[j];
}
}
bitGroupStart = bitGroupStart + 1024;
bitGroupEnd = bitGroupEnd + 1024;
arrayOfMaxAmpVal[i] = maxAmpVal;
frameNumMaxVal[i] = i;
maxAmpVal = 0;
}
I'm implementing the show Deal Or No Deal, there's a class 'box' which in the main file i used to store the random values of the boxes and than, i saved each box in the vector. im trying now to print in the screen the boxes saved in the vector whit the iterator,without succeeding, any help???
//random assignation of pound value to the 22 boxes
for (int e = 1; e < 23; e++)
{
int pos;
bool op = true;
while (op)
{
pos = rand();
if (pos > 0 && pos < 23)
{
if (myArray[pos][1] == 0)
{
myArray[pos][1] = 1;
op = false;
}
}
}
box b(e, myArray[pos][0]); //creating the class box
game_box.push_back(b); //function of the vector to insert a data in it
}
//show boxes
for (auto a = game_box.begin(); a!= game_box.end(); a++)
{
cout << *a << endl;
}
You first need to remove the dereference operator (*) from the a.
Next you need to add an output operator for box. Assuming that box is a class with member data member1 and member2 this would look something like:
friend std::ostream& operator<< (std::ostream &out, const box& b)
{
out << box.memeber1 << " " << box.member2;
}
The key point is that you need to define this operator for every class.
Once you've done that and got it working you might also like to look at this library which defines << operators for all stl containers. This lets you replace
for (auto a = game_box.begin(); a!= game_box.end(); a++)
{
cout << *a << endl;
}
by simply
cout << game_box << endl;
this works..
class Box
{
float pound_contained;
int box_number;
public:
Box(int box_number, float pound_contained);
int getbox_number();
float getpound_contained();
};
int Box::getbox_number()
{
return this->box_number;
}
float Box::getpound_contained()
{
return this->pound_contained;
}
main()
{
vector<Box> game_box;
Box* boxes = &game_box[i];
cout <<boxes->getbox_number()<<endl;
}