-1

I have a function that takes an integer and returns a string that is picked by a switch. The strings are part of a coherent text and I want to be able to add cases to the switch whenever I want to edit the coherent text that the function returns when called for each consecutive int.

So how the function looks like right now:

std::string proceed(int id){
   switch(id){
      case 1: return "Hello";
      case 2: return "my ";
      case 3: return "is";
      case 4: return "Marc";
   }
}

I would like to be able to insert "Name" after case 2 and before case 3. What I do right now is insert another case 3 : return "Name"; and increase ALL the indices below by 1. Which is very fiddly and horrible workflow.

I am looking for a way to do something like the following, in a C++-legal way:

std::string proceed(int id){

   int i = 0;

   switch(id){
      case ++i: return "Hello";
      case ++i: return "my ";
      case ++i: return "is";
      case ++i: return "Marc";
   }
}

here the case statement always (in code) uses the same symbols, which allows to just dump a case anywhere without adjusting ALL the cases.

Is there any syntax or tool to do this in a neat way? (besides using some separate program for automated text editing)

3
  • 1
    I don't think it is possible with switch/case. You can consider another design to make it more robust - like storing key-value pairs in a map/unordered_map.
    – wohlstad
    Commented Jun 14 at 12:41
  • @wohlstad I thought about using a map or simply a vecot but I would like to add some function calls and conditionals here and there inside the cases and containers of std::functions are kinda hard to maintain as well. Thanks still! Commented Jun 14 at 12:47
  • 2
    If you use enums instead of hard coded integrals, then any change to their actual values will be reflected in the case statements. And you can insert new enum lables anywhere in the set without causing problems to the switch block.
    – OldBoy
    Commented Jun 14 at 12:52

1 Answer 1

0

I thought map was the solution , but vector has the easier insert method:

You can encapsulate it if you need more logic:

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

void insertAtIndex(std::vector<std::string>& vec, int index, const std::string& newValue) {
    // Insert the new value at the desired index
    if (index <= vec.size()) {
        vec.insert(vec.begin() + index, newValue);
    } else {
        std::cerr << "Invalid index" << std::endl;
    }
}

And simply call it:


int main() {
    // Create a vector to store the strings
    std::vector<std::string> vec = {"Hello", "my", "is", "Marc"};

    // Insert a new value at index 3
    insertAtIndex(vec, 3, "name");

    // Test the function
    for (int i = 0; i <= vec.size(); ++i) {
        std::cout << vec[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

2
  • You can easily access to elements in order with for(auto &it: vec){std::cout << it << " ";}
    – Ivan
    Commented Jun 14 at 12:56
  • That would avoid the out of bound access (vec[i] with i == vec.size()).
    – Jarod42
    Commented Jun 14 at 21:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.