← Back to list

C++ 11 features and its uses

C++11 is a major revision to the C++ programming language that was standardized by the International Organization for Standardization (ISO)…

Ashwath · 2023-04-11 10:13 · 0 claps · 1.9 min read
#c-11
Open on Medium ↗
Wiki topics: 💻 · Programming

C++ 11 features and its uses

C++11 is a major revision to the C++ programming language that was standardized by the International Organization for Standardization (ISO) in 2011. It introduced several new features and enhancements to the language, including improved type inference, lambda expressions, and more.

Here are some of the key C++11 features and code snippets to explain them in detail:

  1. Type Inference using auto: Auto is a type inference feature introduced in C++11 that allows the compiler to automatically deduce the type of a variable based on its initialization expression.

Example:

auto x = 5; // x is deduced to be of type int auto y = 3.14; // y is deduced to be of type double

Use: It can be used to simplify code and reduce verbosity. It is especially useful when dealing with complex types such as iterators and function return types.

  1. Lambda Expressions: Lambda expressions are anonymous functions that can be defined inline in code. They are a powerful feature that allows you to write concise and expressive code.

Example:

auto lambda = [](int x, int y) -> int { return x + y; }; int result = lambda(2, 3); // result is 5

Use: Lambda expressions are useful when you need to define a function that is used only once and doesn’t need a name.

3.Range-based For Loops: Range-based for loops are a more concise way of iterating over a range of values in C++.

Example:

std::vector<int> nums = {1, 2, 3, 4, 5};
for (int num : nums) {
    std::cout << num << std::endl;
}

Use: Range-based for loops are useful when iterating over a container or range of values, such as an array or a vector.

  1. Smart Pointers: Smart pointers are a type of C++11 pointer that automatically manages memory. They are a safer alternative to raw pointers and can help prevent memory leaks.

Example:

std::unique_ptr<int> ptr = std::make_unique<int>(5);

Use: Smart pointers are useful when working with dynamically allocated memory.

  1. Move Semantics: Move semantics is a feature that allows the efficient transfer of resources between objects, such as when returning an object from a function.

Example:

std::vector<int> v1 = {1, 2, 3, 4, 5};
std::vector<int> v2 = std::move(v1); // v1 is now empty

Use: Move semantics is useful when dealing with large or expensive-to-copy objects, such as containers.

  1. Thread Support Library: C++11 introduced a standardized thread library, which provides a way to create and manage threads in a platform-independent way.

Example:

void foo() {
    std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl;
}
int main() {
    std::thread t(foo);
    t.join();
}

Use: The thread library is useful when you need to write multithreaded code.

In conclusion, these are some of the most important C++11 features that can help you write more efficient, expressive, and safe code. Each feature has its own specific use case, so it’s important to understand when and how to use them properly in your code.


메타데이터
post_id
bd620ece6da8
slug
c-11-features-and-its-uses-bd620ece6da8
url
https://medium.com/@ashwth94/c-11-features-and-its-uses-bd620ece6da8
canonical_url
https://medium.com/@ashwth94/c-11-features-and-its-uses-bd620ece6da8
author_url
https://medium.com/@ashwth94
status
ok
fetched_at
2026-06-29 01:02:39