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

  • SEARCH
  • Home
  • 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 720997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:49:21+00:00 2026-05-14T05:49:21+00:00

vector<int> l; for(int i=0;i<10;i++){ l.push_back(i); } I want the vector to only be able

  • 0
vector<int> l; 
for(int i=0;i<10;i++){ 
   l.push_back(i); 
} 

I want the vector to only be able to store numbers from a specified range (or set).
How can that be done, in general?

In particular, I want to restrict the vector to beonly be able to store single digits.

So, if I do a l[9]++ (in this case l[9] is 9), it should give me an error or warn me. (because 10 is not a single digit number). Similarly, l[0]-- should warn me.

Is there a way to do this using C++ STL vector?

  • 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-14T05:49:21+00:00Added an answer on May 14, 2026 at 5:49 am

    An alternative solution would be to create your own datatype that provides this restrictions. As i read your question I think the restrictions do not really belong to the container itself but to the datatype you want to store. An example (start of) such an implementation can be as follows, possibly such a datatype is already provided in an existing library.

    class Digit
    {
    private:
        unsigned int d;
    public:
        Digit() : d(0) {}
        Digit(unsigned int d)
        {
            if(d > 10) throw std::overflow_error();
            else this->d=d; 
        }
        Digit& operator++() { if(d<9) d++; return *this; }
        ...
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found that this C++ code: vector<int> a; a.push_back(1); a.push_back(2); vector<int>::iterator it = a.begin();
From this code: int x = 5; int other = 10; vector<int*> v_ptr; v_ptr.push_back(&x);
I have a std::vector<int> , and I want to delete the n th element.
Suppose I have a vector<int> myvec and I want to loop through all of
I'm wondering what the best way to write from an std::stringstream into a vector<int>
I'm creating a new class that inherits queue from the STL library. The only
i have a std::vector, namely vector<vector<vector> > > mdata; i want pass data from
I've been playing around with the windows clipboard. I noticed that you can only
I want to clear a element from a vector using the erase method. But
I want to loop through a vector and erase certain elements that correspond to

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.