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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:46:54+00:00 2026-05-25T15:46:54+00:00

using namespace std; vector<vector<T> > vec_collection(3); vec_collection[0]=vector<T>(12); vec_collection[1]=vector<T>(3); vec_collection[2]=vector<T>(14); Suppose I have an empty

  • 0
using namespace std;

vector<vector<T> > vec_collection(3);

vec_collection[0]=vector<T>(12);
vec_collection[1]=vector<T>(3);
vec_collection[2]=vector<T>(14);

Suppose I have an empty collection of 3 vectors and after initialization, I want the first, second, and third vector to be of specific size 12, 3, and 14 say. Is the above code snippet the right way to declare their sizes?

  • 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-25T15:46:55+00:00Added an answer on May 25, 2026 at 3:46 pm

    Your code in line of principle creates separated temporary vector instances that replace the existing zero-size ones, which in theory means that for every of those lines you have a constructor call (create the temporary), a copy-constructor call (copy it over the existing vector) and a destructor call (destroy the temporary).

    Although I think that some optimizations could make it actually better than it sounds, it’s way easier and almost surely more efficient to simply do:

    using namespace std;
    
    vector< vector<T> > vec_collection(3);
    
    vec_collection[0].resize(12);
    vec_collection[1].resize(3);
    vec_collection[2].resize(14);
    

    On the other hand, if you need such vectors to be always of that fixed size, you should replace them with fixed-size data structures (e.g. a struct containing three C-style arrays or std::array), as in @sehe’s answer.

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

Sidebar

Related Questions

#include <iostream> using namespace std; // This first class contains a vector and a
I have the following code #include <iostream> #include <vector> using namespace std; int distance(vector<int>&
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
#include <iostream> #include <vector> using namespace std; int main(void) { int i, s, g;
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void) {
#include <vector> #include <memory> using namespace std; class A { public: A(): i(new int)
I have this main function: #ifndef MAIN_CPP #define MAIN_CPP #include dsets.h using namespace std;
std::vector<float> someOp(void) { using namespace std; vector<float> results; // some operations done to results
#include <boost/algorithm/string.hpp> #include <vector> #include <iostream> #include <string> using namespace std; using namespace boost;

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.