日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

介紹 11 個常用的 C++ 代碼

來源: 責編: 時間:2024-07-11 09:28:57 164觀看
導讀C++是使用最廣泛的編程語言之一。它每天都被數百萬程序員使用,是競爭性編程的首選語言。在這里,我們將列出11 C++代碼片段,可以幫助您解決日常編程問題。因此,事不宜遲,讓我們開始吧。1.查找矢量的大小我們嗯可以使用 size

C++是使用最廣泛的編程語言之一。它每天都被數百萬程序員使用,是競爭性編程的首選語言。在這里,我們將列出11 C++代碼片段,可以幫助您解決日常編程問題。因此,事不宜遲,讓我們開始吧。NRW28資訊網——每日最新資訊28at.com

NRW28資訊網——每日最新資訊28at.com

1.查找矢量的大小

我們嗯可以使用 size() 函數找到向量的大小。NRW28資訊網——每日最新資訊28at.com

#include <bits/stdc++.h>using namespace std; int main(){    vector <int> arr1 = {1, 2, 3, 4};    vector <int> arr2 = {};    vector <float> arr3 = {1.2, 3.8, 3.0, 2.7, 6.6};     cout << "Size of arr1: " << arr1.size() << endl;    cout << "Size of arr2: " << arr2.size() << endl;    cout << "Size of arr3: " << arr3.size() << endl;     return 0;}

輸出:NRW28資訊網——每日最新資訊28at.com

Size of arr1: 4Size of arr2: 0Size of arr3: 5

2.隨機排列數組

我們可以使用 shuffle() 函數在C++中隨機排列數組。NRW28資訊網——每日最新資訊28at.com

#include <bits/stdc++.h>using namespace std; int main(){    vector <int> arr = {1, 2, 3, 4};    unsigned seed = 0;     cout << "Original array:";     for (int ele: arr)    {        cout << ele << " ";    }     cout << endl;     shuffle(arr.begin(), arr.end(), default_random_engine(seed));     cout << "Shuffled array:";     for (int ele: arr)    {        cout << ele << " ";    }     return 0;}

輸出:NRW28資訊網——每日最新資訊28at.com

Original array:1 2 3 4Shuffled array:2 3 1 4

3. 在C++交換兩個變量

我們可以使用C++ STL 庫的內置 swap() 函數交換C++中的兩個變量。NRW28資訊網——每日最新資訊28at.com

#include <bits/stdc++.h>using namespace std; int main(){    int x = 5, y = 10;    string str1 = "MakeUseOf", str2 = "MUO";     cout << "Before Swapping: " << endl;    cout << "x: " << x << endl;    cout << "y: " << y << endl;    cout << "str1: " << str1 << endl;    cout << "str2: " << str2 << endl;     swap(x, y);    swap(str1, str2);     cout << "After Swapping: " << endl;    cout << "x: " << x << endl;    cout << "y: " << y << endl;    cout << "str1: " << str1 << endl;    cout << "str2: " << str2 << endl;     return 0;}

輸出:NRW28資訊網——每日最新資訊28at.com

Before Swapping:x: 5y: 10str1: MakeUseOfstr2: MUOAfter Swapping:x: 10y: 5str1: MUOstr2: MakeUseOf

4.查找數字的位數之和

我們可以使用以下過程找到數字的數字總和:NRW28資訊網——每日最新資訊28at.com

  • 初始化總和變量以存儲結果。
  • 通過對 10 執行模運算來查找數字的余數。
  • 將余數與總和相加。
  • 將數字除以 10。
  • 在數字大于 10 時重復步驟 2 中的過程。
#include <bits/stdc++.h>using namespace std; int main(){    int num = 4635, sum = 0, temp;     while (num != 0)    {        temp = num%10;        sum = sum+temp;        num = num/10;    }     cout << "Sum: " << sum << endl;    return 0;}

輸出:NRW28資訊網——每日最新資訊28at.com

Sum: 18將一個矢量復制到另一個矢量

5. 有多種方法可以將一個向量復制到另一個向量

C++可以使用賦值運算符或將向量作為構造函數傳遞來執行相同的操作。NRW28資訊網——每日最新資訊28at.com

#include <bits/stdc++.h>using namespace std; void printVector(vector <int> vec){    for (auto ele: vec)    {        cout << ele << " ";    }     cout << endl;} int main(){    vector <int> vec = {1, 2, 3, 4, 5};    printVector(vec);     // Method 1: Using Assignment Operator    vector <int> newVec1 = vec;    printVector(newVec1);     // Method 2: By passing vector as constructor    vector <int> newVec2(vec);    printVector(newVec2);     return 0;}

輸出:NRW28資訊網——每日最新資訊28at.com

1 2 3 4 51 2 3 4 51 2 3 4 5

6.查找數組的最大和最小元素

我們可以分別使用max_element()和min_element()函數從數組中找到最大和最小元素。NRW28資訊網——每日最新資訊28at.com

#include <bits/stdc++.h> using namespace std; int main(){    int arr[] = {23, 56, 87, 12, 56};    int size = sizeof(arr)/sizeof(arr[0]);     cout << "Max element: " << *max_element(arr, arr+size) << endl;    cout << "Min element: " << *min_element(arr, arr+size) << endl;     return 0;}

輸出:NRW28資訊網——每日最新資訊28at.com

Max element: 87Min element: 12

7. 在集合中插入元素

我們可以使用 insert() 函數在集合中插入元素。此函數接受元素作為將插入到集合中的參數。NRW28資訊網——每日最新資訊28at.com

#include <bits/stdc++.h>using namespace std; int main(){    set<string> st;     st.insert("Make");    st.insert("Use");    st.insert("Of");    st.insert("Of");     for (auto it = st.begin(); it != st.end(); it++)    {        cout << *it << " ";    }     return 0;}

輸出:NRW28資訊網——每日最新資訊28at.com

Make Of Use

8. 從字符串中刪除重復項

可以使用以下方法從字符串中刪除重復字符:NRW28資訊網——每日最新資訊28at.com

#include <bits/stdc++.h>using namespace std; void removeDuplicateCharacters(char str[], int size){    int newIndex=0;     // Traversing through all the characters    for (int i = 0; i < size; i++)    {        int j;         // Traversing loop from the first character to current character        for (j = 0; j < i; j++)        {            if (str[i] == str[j])            {                break;            }        }         if (j == i)        {            str[newIndex++] = str[i];        }    }     // After removing duplicates, we make    // the vacant part of string to null    str[newIndex] = '/0';}int main(){    char str[] = "MakeUseOf";    int size = strlen(str);     cout << "Original String: " << endl;    cout << str << endl;     removeDuplicateCharacters(str, size);     cout << "New String: " << endl;    cout << str << endl;    return 0;}

輸出:NRW28資訊網——每日最新資訊28at.com

Original String:MakeUseOfNew String:MakeUsOf

9.查找C++字符串的長度

您可以使用 length() 函數查找C++字符串的長度?;蛘撸部梢允褂?size() 函數(它是長度() 函數的別名)。NRW28資訊網——每日最新資訊28at.com

#include <bits/stdc++.h>using namespace std; int main(){    string str1 = "MakeUseOf";    cout << "Length of " << str1 << " : " << str1.length() << endl;     string str2 = "lorem ipsum";    cout << "Length of " << str2 << " : " << str2.size() << endl;     return 0;}

輸出:NRW28資訊網——每日最新資訊28at.com

Length of MakeUseOf : 9Length of lorem ipsum : 11

10.從數組中刪除元素

可以使用以下方法從數組中刪除元素:NRW28資訊網——每日最新資訊28at.com

#include <bits/stdc++.h>using namespace std; int deleteElementFromArray(int arr[], int size, int elementToBeDeleted){    int i, j;     // Search if elementToBeDeleted is present    // in the array or not    for (i = 0; i < size; i++)    {        if (arr[i] == elementToBeDeleted)        {            break;        }    }     // If elementToBeDeleted is found in the array    if (i < size)    {        // We need to reduce the size of the array        // and shift the rest elements        size = size - 1;         for (j = i; j < size; j++)        {            arr[j] = arr[j+1];        }    }     // New array size is returned    return size;} void printArrayElements(int arr[], int size){    for (int i = 0; i < size; i++)    {        cout << arr[i] << " ";    }     cout << endl;}int main(){    int arr[] = {1, 2, 3, 4, 5};    int size = sizeof(arr)/sizeof(arr[0]);     cout << "Original Array: " << endl;    printArrayElements(arr, size);     int elementToBeDeleted = 3;    size = deleteElementFromArray(arr, size, elementToBeDeleted);     cout << "New array: " << endl;    printArrayElements(arr, size);     return 0;}

輸出:NRW28資訊網——每日最新資訊28at.com

Original Array:1 2 3 4 5New array:1 2 4 5 

有時,直接理解復雜的代碼并不容易。您應該遵循一些基本的編程原則,如記錄代碼、重構等,以使代碼更加健壯。NRW28資訊網——每日最新資訊28at.com

11. 迭代向量

您可以通過多種方式循環訪問向量。以下是迭代向量的三種最常用的方法:NRW28資訊網——每日最新資訊28at.com

(1) 使用范圍:NRW28資訊網——每日最新資訊28at.com

#include <bits/stdc++.h>using namespace std; int main(){    vector <int> vec = {1, 2, 3, 4, 5};     // Method 1: Using range for    for (auto element: vec)    {        cout << element << " ";    }     return 0;}使用索引#include <bits/stdc++.h>using namespace std; int main(){    vector <int> vec = {1, 2, 3, 4, 5};     // Method 2: Using indexing    for (int i = 0; i < vec.size(); i++)    {        cout << vec[i] << " ";    }     return 0;}

(2) 使用迭代器的引用:NRW28資訊網——每日最新資訊28at.com

#include <bits/stdc++.h>using namespace std; int main(){    vector <int> vec = {1, 2, 3, 4, 5};     // Method 3: Using reference of the iterator    for (auto it = begin(vec); it != end(vec); it++)    {        cout << *it << " ";    }     return 0;}

以上三個代碼將顯示相同的輸出:NRW28資訊網——每日最新資訊28at.com

1 2 3 4 5

(3) 利用C++代碼片段NRW28資訊網——每日最新資訊28at.com

利用這些C++代碼片段來解決日常編程問題。無論您是使用C++編寫簡單程序還是競爭編程,這些代碼片段都可以派上用場。NRW28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-100336-0.html介紹 11 個常用的 C++ 代碼

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: Python 五分鐘學會五種定時大法

下一篇: 聽說異步和解耦才是消息隊列最有價值的功能

標簽:
  • 熱門焦點
  • Redmi Pad評測:紅米充滿野心的一次嘗試

    從Note系列到K系列,從藍牙耳機到筆記本電腦,紅米不知不覺之間也已經形成了自己頗有競爭力的產品體系,在中端和次旗艦市場上甚至要比小米新機的表現來得更好,正所謂“大丈夫生居
  • 2023 年的 Node.js 生態系統

    隨著技術的不斷演進和創新,Node.js 在 2023 年達到了一個新的高度。Node.js 擁有一個龐大的生態系統,可以幫助開發人員更快地實現復雜的應用。本文就來看看 Node.js 最新的生
  • CSS單標簽實現轉轉logo

    轉轉品牌升級后更新了全新的Logo,今天我們用純CSS來實現轉轉的新Logo,為了有一定的挑戰性,這里我們只使用一個標簽實現,將最大化的使用CSS能力完成Logo的繪制與動畫效果。新logo
  • 一年經驗在二線城市面試后端的經驗分享

    忠告這篇文章只適合2年內工作經驗、甚至沒有工作經驗的朋友閱讀。如果你是2年以上工作經驗,請果斷劃走,對你沒啥幫助~主人公這篇文章內容來自 「升職加薪」星球星友 的投稿,坐
  • 只需五步,使用start.spring.io快速入門Spring編程

    步驟1打開https://start.spring.io/,按照屏幕截圖中的內容創建項目,添加 Spring Web 依賴項,并單擊“生成”按鈕下載 .zip 文件,為下一步做準備。請在進入步驟2之前進行解壓。圖
  • 本地生活這塊肥肉,拼多多也想吃一口

    出品/壹覽商業 作者/李彥編輯/木魚拼多多也看上本地生活這塊蛋糕了。近期,拼多多在App首頁&ldquo;充值中心&rdquo;入口上線了本機生活界面。壹覽商業發現,該界面目前主要
  • 中國家電海外掘金正當時|出海專題

    作者|吳南南編輯|胡展嘉運營|陳佳慧出品|零態LT(ID:LingTai_LT)2023年,出海市場戰況空前,中國創業者在海外紛紛摩拳擦掌,以期能夠把中國的商業模式、創業理念、戰略打法輸出海外,他們依
  • ESG的面子與里子

    來源 | 光子星球撰文 | 吳坤諺編輯 | 吳先之三伏大幕拉起,各地高溫預警不絕,但處于厄爾尼諾大&ldquo;烤&rdquo;之下的除了眾生,還有各大企業發布的ESG報告。ESG是&ldquo;環境保
  • 自研Exynos回歸!三星Galaxy S24系列將提供Exynos和驍龍雙版本

    年初,全新的三星Galaxy S23系列發布,包含Galaxy S23、Galaxy S23+和Galaxy S23 Ultra三個版本,全系搭載超頻版驍龍8 Gen 2,雖同樣采用臺積電4nm工藝制
Top 主站蜘蛛池模板: 新营市| 三江| 随州市| 屯昌县| 丹巴县| 香格里拉县| 永德县| 高台县| 苏尼特右旗| 开封市| 华亭县| 孟连| 偃师市| 尤溪县| 邹城市| 邯郸市| 星子县| 昌邑市| 安丘市| 容城县| 拉孜县| 油尖旺区| 兰坪| 聂荣县| 灌南县| 襄垣县| 陇西县| 许昌县| 台中县| 利川市| 克拉玛依市| 怀柔区| 泸溪县| 葫芦岛市| 班戈县| 固安县| 泉州市| 偃师市| 资中县| 彰化市| 沭阳县|