Same code. Same type. Different behavior. That’s how C++ keeps you on your toes. ⚡ std::vector<int> v1(10, 5); // 10 elements, each = 5 std::vector<int> v2{10, 5}; // 2 elements: 10 and 5 In short: • () → calls std::vector(size, value) • {} → prefers std::initializer_list (initializes from a list of values) Subtle? Yes. Important? Absolutely. #cpp #cplusplus #programming #learninpublic #developers #coding #softwareengineering
C++ Vector Initialization: () vs {}
More Relevant Posts
-
In C++, even if a class has no data members (called an empty class), an object of that class still takes up at least one byte of memory. This is necessary because every object needs a unique memory address. If the size were zero, two different objects could occupy the same memory location, which would cause confusion and errors. So, the minimum size is one byte to give each object its own distinct place in memory. #Cpp #Cplusplus #cpp #cplusplus #Cpp #Cplusplus #programming #learninpublic #developers #coding
To view or add a comment, sign in
-
It’s almost 2026… and many of us still write C++98-style code So let’s fix that. Here are 5 examples of C++98 → Modern C++ that instantly make your code cleaner and more expressive. Modern C++ isn’t about fancy features — it’s safer, clearer, and looks like it belongs in 2026. 💬 Comment your favorite Modern C++ upgrade below! I’ll include the best ones in the next part. #ModernCpp #Cpp #Cpp20 #Cpp23 #CleanCode #Developers #XploreCpp #Programming #SoftwareEngineering
To view or add a comment, sign in
-
📅 Day 45 of #100DaysOfCode Problem: Count Operations to Obtain Zero (LeetCode 2169) 🧩 Approach: 1️⃣ Used a simple loop while both numbers are greater than zero. 2️⃣ In each step, added num1 / num2 to the count — because instead of subtracting repeatedly, we can do it in one go. 3️⃣ Took modulus to simulate the remainder after multiple subtractions. 4️⃣ Swapped numbers and repeated the process until one hit zero. #100DaysOfCode #LeetCode #DSA #ProblemSolving #Cplusplus #CodingChallenge #Algorithms #Programming #DeveloperLife #CodeNewbie #DailyDSA #MathLogic #SoftwareEngineering #TechCommunity #KeepLearning #GrowthMindset #Motivation #CodingJourney
To view or add a comment, sign in
-
-
🚀 Default Arguments in C++ Functions C++ allows functions to have default arguments. These are values that are used if the caller does not provide a value for that argument. Default arguments must be specified from right to left in the parameter list. This feature simplifies function calls and provides flexibility in how functions are used. Default arguments are a convenient way to provide sensible defaults while still allowing the caller to customize the function's behavior. Learn more on our website: https://techielearns.com #c++ #programming #coding #tech #learning #professional #career #development
To view or add a comment, sign in
-
-
Yeah. This is the thing with languages that evolve pragmatically and need to stay backwards-compatible. C++ is not the language Stroustrup would write from scratch. It's funny. To understand why C++ is the glorious mess it is doesn't require technical understanding. You need to understand its history. You need to understand how C grew from BCPL and how BCPL grew from assembly and you need to understand that each decision was not made with the wisdom of hindsight. I wrote a toy language once with a context-free grammar but, even if it wasn't a toy, it would never get adopted. Not for technical reasons. For pragmatic, human reasons.
“I thought I was calling a constructor… but C++ had other plans.” That’s C++’s Most Vexing Parse. ⚡ I wrote: Timer t(); Expecting it to call the constructor and create an object. But C++ quietly treated it as a function declaration returning a Timer. 😅 ✅ Correct way: Timer t{}; // or Timer t; Or if you prefer smart pointers: auto t = std::make_unique<Timer>(); Even something as simple as parentheses can confuse the compiler — and you. #cpp #cplusplus #Cpp #Cplusplus #programming #learninpublic #developers #coding
To view or add a comment, sign in
-
-
“I thought I was calling a constructor… but C++ had other plans.” That’s C++’s Most Vexing Parse. ⚡ I wrote: Timer t(); Expecting it to call the constructor and create an object. But C++ quietly treated it as a function declaration returning a Timer. 😅 ✅ Correct way: Timer t{}; // or Timer t; Or if you prefer smart pointers: auto t = std::make_unique<Timer>(); Even something as simple as parentheses can confuse the compiler — and you. #cpp #cplusplus #Cpp #Cplusplus #programming #learninpublic #developers #coding
To view or add a comment, sign in
-
-
💻 GUESS..! Sometimes, understanding how functions handle variables in C++ can be a real eye-opener 👀 Here’s a short piece of code that looks simple — but the output might not be what you first expect. It’s a great reminder of how pointers, function arguments, and memory handling really work behind the scenes. Can you guess what the output will be? 🤔 #cpp #programming #developers #coding #pointers #cplusplus #learning #outputguess
To view or add a comment, sign in
-
-
What exactly is inside a C++ object? This doc explains the object model and its memory layout. ➜Objects hold only data members directly. ➜A hidden vptr is added for classes with virtual functions. ➜Multiple and virtual inheritance add extra pointers for base class management. ➜The vtable is global and not stored within each object. Understanding this helps write more efficient C++ code. #cpp #cplusplus #memorylayout #programming #coding #softwaredevelopment
To view or add a comment, sign in
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Curly braces are initializer-list greedy.