Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮     
     ❯   

C++ Tutorial

C++ HOMEC++ IntroC++ Get StartedC++ SyntaxC++ OutputC++ CommentsC++ VariablesC++ User InputC++ Data TypesC++ OperatorsC++ StringsC++ MathC++ BooleansC++ If...ElseC++ SwitchC++ While LoopC++ For LoopC++ Break/ContinueC++ ArraysC++ StructuresC++ EnumsC++ ReferencesC++ PointersC++ MemoryManagement

C++ Functions

C++ FunctionsC++ Function ParametersC++ Function OverloadingC++ ScopeC++ RecursionC++ Lambda

C++ Classes

C++ OOPC++ Classes/ObjectsC++ Class MethodsC++ ConstructorsC++ Access SpecifiersC++ EncapsulationC++ Friend FunctionsC++ InheritanceC++ PolymorphismC++ TemplatesC++ FilesC++ Date

C++ Errors

C++ ErrorsC++ DebuggingC++ ExceptionsC++ Input Validation

C++ DataStructures

C++ Data Structures & STLC++ VectorsC++ ListC++ StacksC++ QueuesC++ DequeC++ SetsC++ MapsC++ IteratorsC++ Algorithms

C++ Namespaces

C++ Namespaces

C++ Projects

C++ Projects

C++ How To

C++ Add Two NumbersC++ Random Numbers

C++ Reference

C++ ReferenceC++ KeywordsC++ <iostream>C++ <fstream>C++ <cmath>C++ <string>C++ <cstring>C++ <ctime>C++ <vector>C++ <algorithm>

C++ Examples

C++ ExamplesC++ Real-Life ExamplesC++ CompilerC++ ExercisesC++ QuizC++ SyllabusC++ Study PlanC++ Certificate


C++Data Structures and STL


Data Structures

Data structures are used to store and organize data. Anarray is an example of a data structure, which allows multiple elements to be stored in a single variable.

C++ includes many other data structures as well, each is used to handle data in different ways.

These are part of the C++ STL, which stands for TheStandardTemplateLibrary.


C++ STL

STL is a library that consist of differentdata structures andalgorithmsto effectively store and manipulate data.

If we say that data structures store data, we can say that algorithms are used to solve different problems, often by searching through and manipulating those data structures.

Using the right data structure and algorithm makes your program run faster, especially when working with lots of data.

The most common data structures are:

Data StructureDescription
VectorStores elements like anarray but can dynamically change in size. Adding and removing of elements are usually done at the end. Elements can be accessed by index.
ListStores elements sequentially, where each element is connected to the next. Adding and removing of elements can be done at both ends. Not accessible by index.
StackStores elements in a specific order, called LIFO (Last In, First Out), where elements can only be added and removed from the top. Not accessible by index.
QueueStores elements in a specific order, called FIFO (First In, First Out), where elements are added at the end and removed from the front. Not accessible by index.
DequeStores elements in a double-ended queue, where elements can be added and removed from both ends. Elements can be accessed by index.
SetStores unique elements. Not accessible by index.
MapStores elements in "key/value" pairs. Accessible by keys (not by index).

Which one to use depends on your specific needs. One thing they all have in common is that you must include the appropriate header file to use them:

Example

// Include the vector library
#include <vector>

// Include the list library
#include <list>

// Include the set library
#include <set>

// Include the map library
#include <map>

// Include the stack library
#include <stack>

// Include the queue library
#include <queue>

Here is an example of using vectors, after we have included the<vector> library:

Example

// Create a vector called cars that will store strings
vector<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};

// Print vector elements
for (string car : cars) {
  cout << car << "\n";
}
Try it Yourself »

The next chapters will explain how each data structure works and how to use them.


Key Concepts of the STL

The key components of the STL consist ofcontainers,iterators,andalgorithms, and the relationship between them:

  • Containers are data structures that provides a way to store data, likevectors,lists, etc.
  • Iterators are objects used to access elements of a data structure.
  • Algorithms include functions, likesort() andfind(), that perform operations on data structures throughiterators.

In Computer Science, data structures and algorithms go hand in hand. A data structure is not worth much if you cannot search through it or manipulate it efficiently using algorithms, and algorithms are not worth much without a data structure to work on.

In the following chapters, you will see how everything is connected.





×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp