gpt4 book ai didi

c++ - 包含从 mysql row[x] 构建的固定长度数组的 vector

转载 作者:行者123 更新时间:2023-11-29 20:58:22 24 4
gpt4 key购买 nike

我正在使用 C++ 制作一个 mysql 应用程序。
我想编写一个代码来获取所需的行。
然后我取这一行的值。
我将其放入第一个数组中并遍历列,直到完成该行。
然后我得到下一行,将其放入 array2 中。
然后我将整个 array1 放入数组类型的 vector 中。
我这样做是因为我将循环遍历下一行并将新行值放入相同的两个数组中,但我需要将旧选择的行保留在 vector 中,这些行的大小会随着时间的推移而增加。
我尝试了很多方法,其中之一我认为 vector 中有指针而不是数组的真实数据。
当我尝试获取旧的 vector 值时,我找到了最新的 vector
这是代码:

if(mysql_query(conn, "SELECT * FROM price_ttl limit 10"))
{
print_error (conn,"");
}
res = mysql_store_result(conn);

//this variables are used to store information from res meta data using these functions
totalrows = mysql_num_rows(res);
numfields = mysql_num_fields(res);
/*here we will take value of each column in each row as string
then we will put it in certain position in array of 2 arrays
this array reperesnt 1 row
each array is formed of "12-1"numfields-1 places */
const int maxrows_price_ttl = 2;
const int maxcolumns_price_ttl = 11;//the last cloumn for null value
char two_rows_values_eleven_fields_1strow[maxcolumns_price_ttl];
char two_rows_values_eleven_fields_2ndrow[maxcolumns_price_ttl];
//second we make the vector of multiple arrays for price_mtl table rows
//int maxrows_price_mtl_var ;
//const int maxcolumns_price_mtl = 11;
//we define vector "dynamic array" which is of the type array and call it variable_rows_values_eleven_fields_vector
//the array which represent the type of the vector is called single_row_values_eleven_fields_array
//char *single_row_values_eleven_fields_array;
//here we define the vector its type char* its name variable_rows_values_eleven_fields_vector
std::vector<std::string> variable_rows_values_eleven_fields_vector_string;
std::vector<char*>variable_rows_values_eleven_fields_vector_array;

//this is to make sure we are present in place where mysql_fetch_row will get the desired row "row1"
mysql_data_seek(res, 0);
int rownumber=1;
int columnnumber=0;
std::string row_string="";
//char *row_array[];
//now we want to loop through the result set
for (rownumberall=1; rownumberall < totalrows; rownumberall++)
{
row_string="";
//third we get the value of the current row from result set of current query
//row = mysql_fetch_row (res);//>>>>>>>>>>>>--1-->>>>>--3--
//std::cout <<*(&row )<<std::endl;
//fputc ('\n', stdout);
//printf("Press any key to continue . . . ");
//fputc ('\n', stdout);
//_getch();
for (rownumber=1; rownumber <= maxrows_price_ttl; rownumber++)
{
//third we get the value of the current row from result set of current query
row = mysql_fetch_row (res);//>>>>>>>>>>>>--1-->>>>>--3--

for (columnnumber=0; columnnumber < numfields; columnnumber++)
{
if (rownumber==2)
{
//here we put each coloumn value in its place in array of arrays"which is similar to structure of price table from which we r geting our rows"
//we use rownumber-1 because array 1st place is zero first row is 1 first column is 0
two_rows_values_eleven_fields_2ndrow[columnnumber]= *row[columnnumber] ; //array first elemnt is zero >>>>>>>>>>>>>>--2--a
//std::cout <<two_rows_values_eleven_fields[rownumber-1][columnnumber]<<std::endl;
//printf("Press any key to continue . hawww. . ");
//fputc ('\n', stdout);
//_getch();
}
//row_array[columnnumber]= row[columnnumber] ;
if (rownumber==1)
{
two_rows_values_eleven_fields_1strow[columnnumber]= *row[columnnumber] ;
std::cout <<row_string<<std::endl;
row_string=row_string+row[columnnumber]+"," ;
}
//row_string=row_string+two_rows_values_eleven_fields[0][columnnumber]+"," ;

//std::cout <<row_string<<std::endl;
//fputc ('\n', stdout);
//printf("Press any key to continue . . . ");
//fputc ('\n', stdout);
//_getch();

}


if (rownumber==1)//so that we enter single time in vector
{
row_string.erase(row_string.end()-1);
row_string="'"+row_string+"'" ;

variable_rows_values_eleven_fields_vector_string.push_back(row_string);//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>--2--b
variable_rows_values_eleven_fields_vector_array.push_back(two_rows_values_eleven_fields_1strow);//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>--2--b

// std::cout <<row_string<<std::endl;
// std::cout <<variable_rows_values_eleven_fields_vector_string.back()<<std::endl;
// std::cout <<variable_rows_values_eleven_fields_vector_array.back()<<std::endl;
// std::cout <<variable_rows_values_eleven_fields_vector_array.back()[0]<<std::endl;
// std::cout <<variable_rows_values_eleven_fields_vector_array.back()[1]<<std::endl;
// std::cout <<variable_rows_values_eleven_fields_vector_array.back()[2]<<std::endl;
// fputc ('\n', stdout);
// printf("Press any key to continue . . . ");
// fputc ('\n', stdout);
// _getch();
}
}
//compare their difference for having same sign
//first we need to convert from string to double
//first we make varibles
double price_ttl_pricediff_row_1_double=0;
double price_ttl_pricediff_row_2_double=0;
//here we make string variable for string value of price_ttl pricediff which is extruded from row1 column 6 row 2 column6
std::string str_price_ttl_pricediff_row_1="";
std::string str_price_ttl_pricediff_row_2="";
//this is char pointer required by function strtod to point to begining of string after end of double part of the string to be converted
char *strtodpend;
const char *str_to_char_pointer2;
//here we give variables of price_ttl pricediff its values
str_to_char_pointer2=&two_rows_values_eleven_fields_1strow[5];
price_ttl_pricediff_row_1_double = strtod(str_to_char_pointer2,&strtodpend);
str_to_char_pointer2=&two_rows_values_eleven_fields_2ndrow[5];
price_ttl_pricediff_row_2_double = strtod(str_to_char_pointer2,&strtodpend);
//here this is how to get the sign of each of the two values
//sign(price_ttl_pricediff_row_1_double);
//sign(price_ttl_pricediff_row_2_double);
//this is to show the values of pricediff of price_ttl for test
std::cout <<two_rows_values_eleven_fields_1strow[5]<<std::endl;
std::cout <<two_rows_values_eleven_fields_2ndrow[5]<<std::endl;
std::cout <<str_to_char_pointer2 <<std::endl;
std::cout <<price_ttl_pricediff_row_2_double <<std::endl;

std::cout <<sign(price_ttl_pricediff_row_1_double)<<std::endl;
std::cout <<sign(price_ttl_pricediff_row_2_double) <<std::endl;
std::cout <<price_ttl_pricediff_row_1_double <<std::endl;
std::cout <<price_ttl_pricediff_row_2_double <<std::endl;
//std::cout <<str_price_ttl_pricediff <<std::endl;
//std::cout <<str_price_ttl_timediff <<std::endl;
//std::cout <<insert_into <<std::endl;
//fputc ('\n', stdout);
printf("Press any key to continue . . . ");
fputc ('\n', stdout);
_getch();
//if signs are not the same
if (sign(price_ttl_pricediff_row_1_double)!=sign(price_ttl_pricediff_row_2_double))
{
printf("Press any key to continue . . . ");
fputc ('\n', stdout);
_getch();

//here we calculate values of the row of mtl table from contents of vector
//first we know contents of vector
int vector_size =variable_rows_values_eleven_fields_vector_array.size();//size represents number of rows of price_ttl which will form one row for price_mtl
//we get all information from first and last element in vector
char *first_vector_array_row=variable_rows_values_eleven_fields_vector_array[0];
char *last_vector_array_row=variable_rows_values_eleven_fields_vector_array[(vector_size-1)];

std::cout <<variable_rows_values_eleven_fields_vector_array[0] <<std::endl;
std::cout <<variable_rows_values_eleven_fields_vector_array[1] <<std::endl;
std::cout <<variable_rows_values_eleven_fields_vector_array[2] <<std::endl;


std::cout <<*variable_rows_values_eleven_fields_vector_array[(vector_size-1)] <<std::endl;
std::cout <<variable_rows_values_eleven_fields_vector_string.back()<<std::endl;
std::cout <<variable_rows_values_eleven_fields_vector_string.size()<<std::endl;
std::cout <<vector_size<<std::endl;

variable_rows_values_eleven_fields_vector_string.push_back(row_string);
std::cout <<variable_rows_values_eleven_fields_vector_array[0] [0]<<std::endl;
std::cout <<variable_rows_values_eleven_fields_vector_array[0] [1]<<std::endl;
std::cout <<variable_rows_values_eleven_fields_vector_array[0] [2]<<std::endl;
std::cout <<variable_rows_values_eleven_fields_vector_array[0] [3]<<std::endl;
std::cout <<vector_size <<std::endl;
std::cout <<variable_rows_values_eleven_fields_vector_array[(vector_size-1)][0] <<std::endl;
std::cout <<variable_rows_values_eleven_fields_vector_array[(vector_size-1)] [1]<<std::endl;
std::cout <<variable_rows_values_eleven_fields_vector_array[(vector_size-1)] [2]<<std::endl;
std::cout <<variable_rows_values_eleven_fields_vector_array[(vector_size-1)] [3]<<std::endl;
std::cout <<vector_size <<std::endl;
//fputc ('\n', stdout);
printf("Press any key to continue . . . ");
fputc ('\n', stdout);
_getch();

最佳答案

简而言之,我想制作二维 vector ,它通过 vector [][]获取元素,然后我想使用 vector [][]从 vector 中重新获取元素以获取单个元素或 vector []以获取整行数组是否可以???现在我只需将行作为字符串输入 vector ,然后使用字符串函数通过分隔符检索每个元素如果有人有其他办法我在等

关于c++ - 包含从 mysql row[x] 构建的固定长度数组的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37435027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com