简体   繁体   English

如何在C ++中创建数组的向量(字符串[])

[英]How to create a vector of array ( string [] ) in c++

I have 2 string, from Sqlite3 , ColName and Value. 我有2个字符串,分别来自Sqlite3,ColName和Value。 I want to save each pair of values, i dont know the quantity of ColName/Value , so i use vector. 我想保存每对值,我不知道ColName / Value的数量,所以我使用vector。

is there a way so i can create/push a ColName/Value to the vector of an array 有没有办法让我可以创建/推送ColName / Value到数组的向量

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: 我建议您使用结构的std::vector

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::pairemplace_back来构造pair

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 . 您可以使用类型为std::pair的对象的向量。 For example 例如

std::vector<std::pair<std::string, std::string>> colNameAndValueList;

or a vector of objects of type std::array . 或类型为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 要提出答案,@ huu正确,请使用

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM