1

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.

2
  • 2
    You can use a std::pair as in std::vector<std::pair<std::string, std::string>>. Commented May 1, 2015 at 18:39
  • many thanks! that will do the work! :D @huu Commented May 1, 2015 at 18:51

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

4 Comments

Many thanks! , auto& would do the same as std::vector<std::pair<std::string, std::string> >::iterator it=records.begin(); it!= records.end();it++) ?
In practice it's the same because both let you iterate through the vector. In code it isn't, auto& record is the actual std::pair<std::string, std::string>> object that lives inside the vector.
You're correct, range based for loops are available starting with C++11.
i will do it the old fashioned way, im working on a Mobile POS API that only allows C++98 , thanks :D
2

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 

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.