C++中常用的標志庫

標準庫

C++標準庫是一個強大的工具集,它包含了一組豐富的類和函數,可以幫助開發者進行各種操作,如輸入輸出、字符串操作、數據結構管理、算法實現等。以下是一些常用的C++標準庫及其使用方法。

1. 輸入輸出庫

<iostream>

用于標準輸入輸出操作。

#include <iostream>int main() {int a;std::cout << "Enter a number: ";std::cin >> a;std::cout << "You entered: " << a << std::endl;return 0;
}
<fstream>

用于文件輸入輸出操作。

#include <fstream>
#include <iostream>int main() {std::ofstream outFile("example.txt");outFile << "Hello, World!" << std::endl;outFile.close();std::ifstream inFile("example.txt");std::string content;inFile >> content;std::cout << "File content: " << content << std::endl;inFile.close();return 0;
}

2. 容器庫

<vector>

動態數組容器

#include <vector>
#include <iostream>int main() {std::vector<int> vec = {1, 2, 3, 4, 5};vec.push_back(6);for (int v : vec) {std::cout << v << " ";}std::cout << std::endl;return 0;
}
<list>

雙向鏈表容器。

#include <list>
#include <iostream>int main() {std::list<int> lst = {1, 2, 3, 4, 5};lst.push_back(6);for (int v : lst) {std::cout << v << " ";}std::cout << std::endl;return 0;
}
<map>

鍵值對容器。

#include <map>
#include <iostream>int main() {std::map<std::string, int> myMap;myMap["one"] = 1;myMap["two"] = 2;for (const auto& pair : myMap) {std::cout << pair.first << ": " << pair.second << std::endl;}return 0;
}

3. 算法庫

<algorithm>

常用算法函數。

#include <algorithm>
#include <vector>
#include <iostream>int main() {std::vector<int> vec = {3, 1, 4, 1, 5, 9};// 排序std::sort(vec.begin(), vec.end());for (int v : vec) {std::cout << v << " ";}std::cout << std::endl;// 查找if (std::binary_search(vec.begin(), vec.end(), 5)) {std::cout << "Found 5" << std::endl;}return 0;
}

4. 字符串庫

<string>

字符串操作。

#include <string>
#include <iostream>int main() {std::string str = "Hello";str += ", World!";std::cout << str << std::endl;std::string substr = str.substr(0, 5);std::cout << "Substring: " << substr << std::endl;return 0;
}

5. 數學庫

<cmath>

數學函數。

#include <cmath>
#include <iostream>int main() {double x = 3.14;std::cout << "sin(x) = " << sin(x) << std::endl;std::cout << "cos(x) = " << cos(x) << std::endl;std::cout << "sqrt(16) = " << sqrt(16) << std::endl;return 0;
}

6. 時間庫

<chrono>

時間處理。

#include <chrono>
#include <iostream>
#include <thread>int main() {auto start = std::chrono::high_resolution_clock::now();std::this_thread::sleep_for(std::chrono::seconds(1));auto end = std::chrono::high_resolution_clock::now();std::chrono::duration<double> duration = end - start;std::cout << "Elapsed time: " << duration.count() << " seconds" << std::endl;return 0;
}

7. 多線程庫

<thread>

多線程處理。

#include <thread>
#include <iostream>void threadFunction() {std::cout << "Thread is running" << std::endl;
}int main() {std::thread t(threadFunction);t.join();  // 等待線程完成return 0;
}

8. 智能指針庫

<memory>

智能指針管理。

#include <memory>
#include <iostream>int main() {std::unique_ptr<int> ptr = std::make_unique<int>(10);std::cout << "Unique pointer value: " << *ptr << std::endl;std::shared_ptr<int> sharedPtr = std::make_shared<int>(20);std::cout << "Shared pointer value: " << *sharedPtr << std::endl;return 0;
}

9. 其他有用的庫

<utility>

包含一些實用工具,如 std::pairstd::move

#include <utility>
#include <iostream>int main() {std::pair<int, std::string> p(1, "one");std::cout << "Pair: " << p.first << ", " << p.second << std::endl;int x = 10;int y = std::move(x);std::cout << "y: " << y << std::endl;return 0;
}

10、<atomic>

<atomic> 庫提供了原子操作的類和函數,用于多線程環境下對共享變量進行安全的操作。

#include <atomic>
#include <iostream>
#include <thread>std::atomic<int> counter(0);void increment() {for (int i = 0; i < 10000; ++i) {counter++;}
}int main() {std::thread t1(increment);std::thread t2(increment);t1.join();t2.join();std::cout << "Counter value: " << counter << std::endl;return 0;
}

11、<mutex>

<mutex> 庫提供了互斥鎖和其他同步原語,用于在多線程程序中實現線程安全的訪問控制

#include <mutex>
#include <iostream>
#include <thread>std::mutex mtx;void threadFunction() {std::lock_guard<std::mutex> lock(mtx);std::cout << "Thread is running" << std::endl;
}int main() {std::thread t(threadFunction);t.join();return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/35812.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/35812.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/35812.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

2024年最新通信安全員考試題庫

61.架設架空光纜&#xff0c;可使用吊板作業的情況是&#xff08;&#xff09;。 A.在2.2/7規格的電桿與墻壁之間的吊線上&#xff0c;吊線高度5m B.在2.2/7規格的墻壁與墻壁之間的吊線上&#xff0c;吊線高度6m C.在2.2/7規格的電桿與電桿之間的吊線上&#xff0c;吊線高度…

[leetcode]24-game

. - 力扣&#xff08;LeetCode&#xff09; class Solution { public:static constexpr int TARGET 24;static constexpr double EPSILON 1e-6;static constexpr int ADD 0, MULTIPLY 1, SUBTRACT 2, DIVIDE 3;bool judgePoint24(vector<int> &nums) {vector&l…

【C++LeetCode】【熱題100】三數之和【中等】-不同效率的題解【6】

題目&#xff1a; 暴力方法&#xff1a; class Solution { public:vector<vector<int>> threeSum(vector<int>& nums) {vector<vector<int>> res;std::unordered_set<std::string> uniqueValues;//保證結果唯一for(int i0;i<n…

工商業儲能柜用的Acrel-2000ES儲能能量管理系統-安科瑞 蔣靜

概述 Acrel-2000ES儲能能量管理系統&#xff0c;專門針對工商業儲能柜、儲能集裝箱研發的一款儲能EMS&#xff0c;具有完善的儲能監控與管理功能,涵蓋了儲能系統設備(PCS、BMS、電表、消防、空調等)的詳細信息&#xff0c;實現了數據采集、數據處理、數據存儲、數據查詢與分析…

PHP米表域名出售管理源碼帶后臺

源碼介紹 html5米表源碼PHP域名銷售程序安裝方法&#xff1a; 本站已測試,各項功能正常,功能易用,不復雜,非常適合個人米表使用 1、所有文件傳至網站目錄 2、瀏覽器執行http://你的訪問網址/install 3、輸入mysql帳號及密碼信息&#xff0c;提交安裝 源碼截圖 源碼下載 …

MySQL程序使用的選項文件

MySQL程序使用的選項文件如下&#xff1a; 顯示幫助消息并退出。 在具有多個網絡接口的計算機上&#xff0c;使用此選項可以選擇用于連接MySQL服務器的接口。 安裝字符集的目錄。 如果可能&#xff0c;壓縮客戶端和服務器之間發送的所有信息。 從MySQL 8.0.18開始&#xff0c;…

GPOPS-II教程(3): 航天器最優控制問題

文章目錄 問題描述GPOPS代碼main functioncontinuous functionendpoint function完整代碼代碼仿真結果 最后 問題描述 例子出自論文 Direct solution of nonlinear optimal control problems using quasilinearization and Chebyshev polynomials&#xff08;DOI&#xff1a;1…

新手選擇代理IP時這幾點誤區一定要避開!

在選擇代理IP時&#xff0c;許多用戶可能會因為對代理IP的認識不足或受到一些誤導&#xff0c;而陷入一些常見的誤區。這些誤區不僅可能導致用戶無法達到預期的效果&#xff0c;還可能帶來一些不必要的風險。下面&#xff0c;IPIDEA代理IP就與大家一同分析在選擇代理IP時需要避…

國企:2024年6月中國鐵路相關招聘信息,6.27截止

中國鐵路濟南局集團有限公司2024年度 招聘普通高校本科及以上學歷畢業生公告(三) 中國鐵路濟南局集團有限公司根據企業發展需要,擬招聘普通高等院校本科及以上學歷畢業生,現將有關事項公告如下: 一、招聘計劃 本次招聘崗位均為生產一線操作技能崗位,具體崗位、專業要求…

【教資優秀作文】

目錄 不沉湎于過去&#xff0c; 向未來進發 轉變思維方式&#xff0c;風景這邊獨好 一英尺的距離 面對逆境&#xff0c;智者生存 機遇與準備 1. 巴西足球名將貝利在足壇初露鋒芒時 &#xff0c;一個記者問他&#xff1a;“你哪一個球踢得最好&#xff1f; ” 他回答說&am…

說說ThreadLocal的實現原理

ThreadLocal是什么&#xff1f; ThreadLocal是Java中的一個類&#xff0c;用于創建線程局部變量和解決線程安全。每個線程都有自己獨立的變量副本&#xff0c;彼此之間互不影響。它的主要作用是在多線程環境下&#xff0c;確保每個線程都有自己的變量實例&#xff0c;避免了變…

Retrofit類型安全的HTTP客戶端庫(json)

簡介 Retrofit是Square公司開發的一個類型安全的HTTP客戶端庫&#xff0c;用于Android和Java平臺&#xff0c;它使得與Web服務的交互變得更加簡單快捷。Retrofit將HTTP API轉換成Java接口&#xff0c;讓你可以用更簡潔的代碼形式調用RESTful API&#xff0c;Android網絡編程重點…

在前端開發過程中如果函數參數很多,該如何精簡

1. 在前端開發過程中如果函數參數很多&#xff0c;該如何精簡 1.1. 對象參數&#xff08;對象字面量&#xff09;&#xff1a;1.2. 默認參數和解構賦值&#xff1a;1.3. 使用類或構造函數&#xff1a;1.4. 利用閉包或者高階函數&#xff1a;1.5. 利用ES6的擴展運算符&#xff1…

【LeetCode】每日一題:反轉鏈表

題解思路 循環的方法需要注意prev應該是None開始&#xff0c;然后到結束的時候prev是tail&#xff0c;遞歸的思路很難繞過彎來&#xff0c;主要在于很難想清楚為什么可以返回尾節點&#xff0c;需要多做遞歸題&#xff0c;以及遞歸過程中&#xff0c;可以不使用尾節點來找當前…

Nuxt3 的生命周期和鉤子函數(二)

title: Nuxt3 的生命周期和鉤子函數&#xff08;二&#xff09; date: 2024/6/26 updated: 2024/6/26 author: cmdragon excerpt: 摘要&#xff1a;本文深入介紹了Nuxt.js框架中幾個關鍵的生命周期鉤子函數&#xff0c;包括app:redirected&#xff08;SSR環境下重定向前觸發…

20240626讓飛凌的OK3588-C開發板在相機使用1080p60分辨率下預覽

20240626讓飛凌的OK3588-C開發板在相機使用1080p60分辨率下預覽 2024/6/26 15:15 4.2.1 全編譯測試 在源碼路徑內&#xff0c;提供了編譯腳本 build.sh&#xff0c;運行該腳本對整個源碼進行編譯&#xff0c;需要在終端切換到解壓 出來的源碼路徑&#xff0c;找到 build.sh 文件…

6.26作業

1.整理思維導圖 2.統計家目錄下.c文件的個數 ls ~/*.c | wc -l 3.終端輸入一個.sh文件&#xff0c;判斷文件是否由可執行權限&#xff0c;如果有可執行權限運行腳本&#xff0c;沒有可執行權限添加可執行權限后&#xff0c;再運行腳本 #!/bin/bash read -p "請輸入一個.…

spring模塊(二)SpringBean(2)InitializingBean

一、介紹 InitializingBean是Spring框架提供的一個接口&#xff0c;用于在Bean初始化完成后執行特定的初始化邏輯。 二、使用 1、使用方法 1.1、實現InitializingBean接口 可以讓Bean實現該接口&#xff0c;并重寫其afterPropertiesSet()方法 1.2、注冊 也即讓bean初始化…

從官方源碼精簡出第1個FreeRTOS程序

一、下載官方源碼 1、打開百度搜索freerots&#xff0c;找到官網:FreeRTOS官網 2、將源碼解壓到沒有中文目錄的路徑下 二、刪減目錄 1、刪除FreeRTOS-Plus和tools 2、刪除FreeRTOS/Demo下除CORTEX_STM32F103_Keil外的所有文件 3、刪除FreeRTOS\Source\portable下除RVDS和MemM…

vue2面試題——API

1. $set this.$set(目標對象target&#xff0c;改的位置&#xff0c;最終數據) /* 數據更新了而視圖沒有更新的情況 */ <template><div>{{ arr }}<button clickbtn>按鈕</button></div> </template> <script> export default {name:…