Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6092183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:24:48+00:00 2026-05-23T12:24:48+00:00

Suppose I have two containers of elements: std::vector<std::string> foo = { aa, bb, cc

  • 0

Suppose I have two containers of elements:

std::vector<std::string> foo = { "aa", "bb", "cc" };
std::vector<std::string> bar = { "dd", "ee" };

I pass around indecies into foo and bar between many classes that operate on these two containers. However, I find that I have to be very careful not to confuse which indecies are associated with which container, and sometimes fail in this and then have a lot of trouble finding logical errors that result.

So, suppose I have two indecies:

int id_foo = 1;
int id_bar = 0;

Is there a simple design pattern which helps prevent cases where I try to call bar[id_foo] by “binding” id_foo to foo.

Basically, I’m trying to figure out the simplest way to disambiguate between foo and bar. I could create separate classes for each and then just define iterators into those classes. But I would like to avoid creating a separate class for every container like foo and bar for which I want this “safety”.

The question is asking for something like “index safety” (analogous to type safety). The way I’m currently accomplishing this is by naming my indecies in such a way as to indicate which container they are associated with.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:24:49+00:00Added an answer on May 23, 2026 at 12:24 pm

    There isn’t really a design pattern for differentiating what an index indexes into. There are some conventions.

    You could use typedefs to differentiate them. That is, typedef an int to be a FooIndex, and similarly for a BarIndex. You could also use a bit of Hungarian notation for the names (I know people hate that, but this sort of thing is really what it was intended for). You already seem to be using some of it, but not in a useful way. Consider this naming convention:

    int fooCurrentKey = 1;
    int barCurrentKey = 0;
    

    This lets you know that the first index is for list foo and the second is for list bar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two versions v0.1 and v0.2. In between, there are about 20
Suppose I have two forms: Form1 and Form2. Form2 has a text control named
Suppose we have two modules with cyclic dependencies: # a.py import b def f():
I have a page which I am supposed to make accessible without too many
I'm learning Android by writing a game and have a problem with the graphics
I have a Facebook game titled Rails Across Europe. It is written in PHP/MySQL/Facebook
Hi I have a small problem. I am coding a program that displays the
TLDR : Can the onreadystatechange event be emulated somehow else for synchronous requests without
I am getting confused with my code and not sure how to implement what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.