简体   繁体   English

如何在for循环中顺序访问c样式字符串变量

[英]How to access c style string variables sequentially in for loop

I have a long list of some good old fashioned c style strings: 我列出了一些很好的老式c样式字符串:

const char * p1key = PROPERTY_MAX_THREADS;
const char * p1value  = "12";
const char * p2key = PROPERTY_MAX_FRAMES;
const char * p2value  = "400";
const char * p3key = PROPERTY_MAX_FRAMEMEMORY;
const char * p3value  = "140";
...

Then I do some stuff with them: 然后我对他们做一些事情:

// write p1, p2, p3, pn to disk in fancy format

At the end I want to be able to write a loop and compare the written values to the original values. 最后,我希望能够编写一个循环并将写入的值与原始值进行比较。

int numProperties = 20;
for (int i = 0; i < numProperties; ++i) {
  // on the first iteration, access p1 key/value
  // on the second, access p2 key/value
  // ...
}

How can I access p1 on the first iteration, p2 on the second, etc? 如何在第一次迭代中访问p1,在第二次迭代中访问p2,依此类推? Would an array of pointers help? 指针数组会有所帮助吗? I'm struggling to come up with the syntax to make this work. 我正在努力提出语法来完成这项工作。 Any help would be very much appreciated. 任何帮助将不胜感激。

Edit: 编辑:

I would consider the best answer to show both the C and C++ way 我认为最好的答案是同时显示C和C ++方式

INTRODUCTION 介绍

You'd have to store the pointers in some sort of container to be able to iterate over them in the manner as you propose. 您必须将指针存储在某种容器中,以便能够按照建议的方式遍历它们。

Since you are dealing with pairs, std::pair from <utility> seems like a perfect match. 由于您正在处理配对,因此<utility> std::pair似乎是完美的匹配。 Wrapping these std::pair s in a container such as std::vector will make it very easy to iterate over them in a clean manner. 将这些std::pair包裹在诸如std::vector类的容器中,将很容易以干净的方式遍历它们。


SAMPLE IMPLEMENTATION 样品实施

#include <iostream>
#include <utility>
#include <vector>

#define PROPERTY_MAX_THREADS     "max_threads"
#define PROPERTY_MAX_FRAMES      "max_frames"
#define PROPERTY_MAX_FRAMEMEMORY "max_fmemory"

const char * p1key = PROPERTY_MAX_THREADS;
const char * p1value  = "12";
const char * p2key = PROPERTY_MAX_FRAMES;
const char * p2value  = "400";
const char * p3key = PROPERTY_MAX_FRAMEMEMORY;
const char * p3value  = "140";

int
main (int argc, char *argv[])
{

  std::vector<std::pair<char const *, char const *>> properties {
    { p1key, p1value }, { p2key, p2value }, { p3key, p3value }
  };

  std::cout << "properties:\n";

  for (auto& it : properties) {
    std::cout << "  " << it.first << " = " << it.second << "\n";
  }
}

properties:
  max_threads = 12
  max_frames = 400
  max_fmemory = 140

I TRIED THE ABOVE BUT IT DOESN'T COMPILE, WHY? 我尝试了以上方法,但没有编译,为什么?

The previously written snippet makes use of features introduced in C++11, if you are unable to compile such code you will need to resort to functionality that your compiler does provide. 先前编写的代码段利用了C ++ 11中引入的功能,如果您无法编译此类代码,则需要使用编译器确实提供的功能。

Below is a modified implementation that can be compiled by any compiler that supports C++03: 下面是一个修改的实现,可以由任何支持C ++ 03的编译器进行编译:

  int const PROPERTIES_LEN = 3;

  std::pair<char const *, char const*> properties[PROPERTIES_LEN] = {
    std::make_pair (p1key, p1value),
    std::make_pair (p2key, p2value),
    std::make_pair (p3key, p3value)
  };

  for (int i = 0; i < PROPERTIES_LEN; ++i) {
    std::cout << properties[i].first << " = " << properties[i].second << "\n";
  }

You tagged it C++, so I'm going to give the C++ suggestion. 您将其标记为C ++,所以我将提出C ++建议。

#include <iostream>
#include <vector>
#include <utility>

#define PROPERTY_MAX_THREADS        "1"
#define PROPERTY_MAX_FRAMES         "2"
#define PROPERTY_MAX_FRAMEMEMORY    "3"

const char * p1key = PROPERTY_MAX_THREADS;
const char * p1value  = "12";
const char * p2key = PROPERTY_MAX_FRAMES;
const char * p2value  = "400";
const char * p3key = PROPERTY_MAX_FRAMEMEMORY;
const char * p3value  = "140";


int main() {
    using namespace std;
    vector<pair<const char*,const char *>> collection =
        {{p1key,p1value},{p2key,p2value},{p3key,p3value}};

    for(auto &ele : collection){
        cout << "key:" << ele.first
             << "value:" << ele.second << endl;
    }
    return 0;
}

alternatively just declare it as a collection from the beginning 或者只是从一开始就将其声明为集合

#include <iostream>
#include <string>
#include <vector>
#include <utility>

#define PROPERTY_MAX_THREADS        "1"
#define PROPERTY_MAX_FRAMES         "2"
#define PROPERTY_MAX_FRAMEMEMORY    "3"

int main() {
    using namespace std;
    vector<pair<const string,const string>> collection =
        {
            {PROPERTY_MAX_THREADS,      "12" },
            {PROPERTY_MAX_FRAMES,       "400"},
            {PROPERTY_MAX_FRAMEMEMORY,  "140"}
        };

    for(auto &ele : collection){
        cout << "key:" << ele.first
             << " value:" << ele.second << endl;
    }
    return 0;
}

In C you can do this way. 在C语言中,您可以这样做。

#define STRA_END 0
const char* keyArray[] = {
    "string1",
    "string2",
    "string3",
    STRA_END
}
const char* valueArray[] = {
    "string1",
    "string2",
    "string3",
    STRA_END
}
main(){
    int i;
    for( i=0; keyArray[i]!=0; ++i )
        doSometingToString(keyArray[i], valueArray[i]);
}

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

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