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 6011007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:11:51+00:00 2026-05-23T02:11:51+00:00

I use code like this: const vector<filterStat> someArray={ {Ana,1}, {Bob,2}, {Charlie,5}, }; static const

  • 0

I use code like this:

const vector<filterStat> someArray={
        {"Ana",1},
        {"Bob",2},
        {"Charlie",5},
};
static const int knElements=filterStats.size();

Ignore the kn prefix, it is my way of saying constant, size.
I found it useful because I don;t have to change or calculate knElements when I change the initialization of the vector. But the problem is that using a const vector bothers me because vector is resizable array so it feels wrong. BTW if you are wondering why I need it-long story, it’s kind of a map, but I dont do any searching or inserting, just “for each” so array is the best choice.

EDIT:
I changed my cod to this and it compiles:

const   filterStat filterStats[]={
//...
};

static const int knFilterStats=sizeof(filterStats)/sizeof(filterStats[0]);
static_assert(sizeof(filterStats),"Can't run statistics-no filterStats exists");

I had no idea that you can do [] in c++. Sorry for stupid question, I hope it helps somebody.

  • 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-23T02:11:52+00:00Added an answer on May 23, 2026 at 2:11 am

    A const vector is a perfectly good thing to have – your code is the right way to do things.

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

Sidebar

Related Questions

When using XML serialization in C#, I use code like this: public MyObject LoadData()
Assuming sr is an IEnumerable<string> , I want to use code like this to
What criteria should I use to decide whether I write VBA code like this:
I use C# code more-or-less like this to serialize an object to XML: XmlSerializer
This has been bothering me lately- when I use some code like below to
A common piece of code I use for simple string splitting looks like this:
If I have code with nested objects like this do I need to use
Usually like this: #include <boost/assign/std/vector.hpp> vector<int> v; v += 1,2,3,4,5; Except for a: #include
I have a simple class like this: class A { public: void f(const int&
struct G{ G&operator()(const int**a) { v<<a; std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout, )); return *this; } friend std::vector<int>&operator<<(std::vector<int>&v,const int**

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.