当前位置:网站首页>STL learning
STL learning
2022-04-22 03:15:00 【Red fog】
One 、string
1.char * and string The difference between
char* Is a pointer to the address of the character array , The system provides a string.h The header file declares many string manipulation functions ,strlen strcat strcmp strcpy…
string Is a class , This class encapsulates the operation functions , Make the operation of string more flexible , More ways , More reasonable management ;
string This class , We do not need to consider the allocation and release of memory , Don't worry about cross-border collapse ,string Have done relevant processing .
2.string The definition of
typedef basic_string<char,char_traits<char>,allocator<char>> string;
3. Constructors
string();
string(size_type length,char ch);
string(const char *str);
string(const char *str,size_type length);//str The first few
string(string &str,size_type index,size_type length);// In the middle
string(const string &str);// Copy structure
4. Common functions
size();
Two 、vector
1.C++ STL And vector Of capacity and size Attribute difference
size It is the present. vector The actual size of the container , That is, how many containers the container currently has .
capacity It means that realloc The maximum number of elements allowed before , That is, the pre allocated memory space .
Of course , These two properties correspond to two methods :resize() and reserve().
Use resize() The object memory space in the container really exists .
Use reserve() Just modified capacity Value , The objects in the container have no real memory space ( Space is " wild " Of ).
Remember to use [] Operator to access the objects in the container , There is likely to be an array out of bounds problem .
in the light of capacity This attribute ,STL Other containers in , Such as list map set deque, Because the memory of these containers is hashed , So it won't happen realloc() Call status of , So we can think of capacity Attributes are meaningless for these containers , Therefore, these containers do not have this attribute at design time .
stay STL in , Have capacity The only container for attributes is vector 、deque and string.deque The elements of are stored continuously in memory , Same ownership capacity attribute ,deque Have vector All functions of , And you can insert data in the head and tail .
2.vector data(num); When defining, use num Specify the number of elements , This allows random access to these elements later .
#include <iostream>
#include <vector>
using namespace std; // using namespace std std
int main()
{
vector <int> data; // Define vector objects int n;
int temp;
for (int i = 0;i < 5;++i)
{
cin >> temp;// Input with auxiliary variables
data.push_back(temp);
}
for (int i = 0;i < 5;++i)
{
cout << data[i] << endl;
}
}
#include <iostream>
#include <vector>
using namespace std; // using namespace std std
int main()
{
vector <int> data(5); // Define vector objects int n;
for (int i = 0;i < 5;++i)
{
cin >> data[i];// Direct access to input
}
for (int i = 0;i < 5;++i)
{
cout << data[i] << endl;
}
}
3.vector The definition of
template <class T, class Allocator = allocator <T>>// Vector template
class vector {
...
}
版权声明
本文为[Red fog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211350460476.html
边栏推荐
- Flutter03 dart asynchronous
- Polyfill是什么?
- C language to achieve three chess
- moudle中的activity跳转到主项目的activity
- Allegro gerber文件路径设定
- How to take sonar as an example to create a test step applicable to all enterprises
- Source code encryption Product Guide
- Joint primary key and composite primary key creation of Oracle
- Driverless virtual simulation (14) -- traffic sign recognition in image processing 2
- [experience] TF1 X migrate to TF2 X tutorial
猜你喜欢

ESP32_ GPIO input, output and interrupt

leetcode:297. Serialization and deserialization of binary tree

Golang dependency injection wire. When executing the wire command, an error is reported: bash: Wire: command not found

Competition conference of the most fully quantified hardware facilities in the whole network

72. Edit distance

824. Goat Latin (string segmentation + string substitution)

Use of uiautomator2 automated test tool

Network and multimedia knowledge (3)

Driverless virtual simulation (14) -- traffic sign recognition in image processing 2

全网最全量化硬件设施比拼大会
随机推荐
leetcode:297. Serialization and deserialization of binary tree
Redis event driven framework (Part 1): when to use select, poll and epoll?
7、Request_Response
How to stimulate open thinking through communication
2022-4-21作业
Introduction of explanatory variables in code refactoring
twenty-seven. Package (import)
Sword finger offer special breakthrough version 91. Painting the house
二十七.包(import)
Ros2 learning notes (III) -- collect and release images of virtual simulation environment
Oracle的联合主键和复合主键创建
Saas. Data isolation and persistence scheme
Nacos access message 404
剑指offer 专项突破版 93、最长斐波那契数列
Flutter03 dart asynchronous
Promise me not to empty if (obj = null)
Performance test and comparison of oceanbase and MariaDB using dbt-3
Flutter03-Dart异步
[experience] TF1 X migrate to TF2 X tutorial
How to take sonar as an example to create a test step applicable to all enterprises