I have 2 string, from Sqlite3 , ColName and Value. I want to save each pair of values, i dont know the quantity of ColName/Value , so i use vector.
is there a way so i can create/push a ColName/Value to the vector of an array
code:
std::vector<std::string[3]> colNameAndValueList;//this doesnt work
string colName="ID";
string value="122001";
colNameAndValueList.push_back(std::string(colName,value));//im lost here
i dont know if i should use hash or struct, can anyone give me an advice?
thanks.
I recommend that you use a std::vector
of structure:
struct Name_Value
{
std::string name;
std::string value;
};
typedef std::vector<Name_Value> Name_Value_Container;
This is a lot easier to read, understand, and implement.
There are many ways to skin this cat. You can use std::pair
and emplace_back
to construct the pair
in place when you're inserting values into your array:
std::vector<std::pair<std::string, std::string>> records;
std::string column = "hello";
std::string value = "world";
records.emplace_back(column, value); // Use existing strings
records.emplace_back("new", "value"); // Use c-string literals
for (auto& record : records) {
std::cout << record.first << ": " << record.second << std::endl;
}
/*
* Prints:
* hello: world
* new: value
*/
Here's a working example .
You can use a vector of objects of type std::pair
. For example
std::vector<std::pair<std::string, std::string>> colNameAndValueList;
or a vector of objects of type std::array
. For example
std::vector<std::array<std::string, 2>> colNameAndValueList;
Ordinary arrays do not have the copy assignment operator. So it is better not to use them in standard containers.
Here is a demonstrative program
#include <iostream>
#include <vector>
#include <array>
int main()
{
{
std::vector<std::pair<std::string, std::string>> colNameAndValueList;
colNameAndValueList.push_back( { "ID", "122001" } );
for ( const auto &p : colNameAndValueList )
{
std::cout << p.first << ' ' << p.second << std::endl;
}
}
{
std::vector<std::array<std::string, 2>> colNameAndValueList;
colNameAndValueList.push_back( { "ID", "122001" } );
for ( const auto &a : colNameAndValueList )
{
for ( const auto &s : a ) std::cout << s << ' ';
std::cout << std::endl;
}
}
return 0;
}
The program output is
ID 122001
ID 122001
To put in an answer, @huu has it right, use an
std::vector<std::pair<std::string, std::string>> myVector
std::pair("ID", "122001") mypair;
myVector.push_back(mypair);
Or a user defined struct.
// In your .h file
struct myPair {
std::string one;
std::string two;
};
// in your .c file
myPair res;
res.one = "ID";
res.two = "122001";
std::vector<myPair> myVector;
myVector.push_back(res);
Try this:
vector<pair<string, string>> colNameAndValueList;
string colName = "ID";
string value = "122001";
colNameAndValueList.push_back( { colName, value } );
If you need more than two strings in your record then you may use:
vector<vector<string>> colNameAndValueList;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.