简体   繁体   中英

How to access c style string variables sequentially in for loop

I have a long list of some good old fashioned c style strings:

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? 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

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. 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.


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.

Below is a modified implementation that can be compiled by any compiler that supports 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.

#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.

#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]);
}

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.

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