当前位置:首页 > 科技  > 软件

介绍 11 个常用的 C++ 代码

来源: 责编: 时间:2024-07-11 09:28:57 226观看
导读C++是使用最广泛的编程语言之一。它每天都被数百万程序员使用,是竞争性编程的首选语言。在这里,我们将列出11 C++代码片段,可以帮助您解决日常编程问题。因此,事不宜迟,让我们开始吧。1.查找矢量的大小我们嗯可以使用 size

C++是使用最广泛的编程语言之一。它每天都被数百万程序员使用,是竞争性编程的首选语言。在这里,我们将列出11 C++代码片段,可以帮助您解决日常编程问题。因此,事不宜迟,让我们开始吧。cDg28资讯网——每日最新资讯28at.com

cDg28资讯网——每日最新资讯28at.com

1.查找矢量的大小

我们嗯可以使用 size() 函数找到向量的大小。cDg28资讯网——每日最新资讯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;}

输出:cDg28资讯网——每日最新资讯28at.com

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

2.随机排列数组

我们可以使用 shuffle() 函数在C++中随机排列数组。cDg28资讯网——每日最新资讯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;}

输出:cDg28资讯网——每日最新资讯28at.com

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

3. 在C++交换两个变量

我们可以使用C++ STL 库的内置 swap() 函数交换C++中的两个变量。cDg28资讯网——每日最新资讯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;}

输出:cDg28资讯网——每日最新资讯28at.com

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

4.查找数字的位数之和

我们可以使用以下过程找到数字的数字总和:cDg28资讯网——每日最新资讯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;}

输出:cDg28资讯网——每日最新资讯28at.com

Sum: 18将一个矢量复制到另一个矢量

5. 有多种方法可以将一个向量复制到另一个向量

C++可以使用赋值运算符或将向量作为构造函数传递来执行相同的操作。cDg28资讯网——每日最新资讯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;}

输出:cDg28资讯网——每日最新资讯28at.com

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

6.查找数组的最大和最小元素

我们可以分别使用max_element()和min_element()函数从数组中找到最大和最小元素。cDg28资讯网——每日最新资讯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;}

输出:cDg28资讯网——每日最新资讯28at.com

Max element: 87Min element: 12

7. 在集合中插入元素

我们可以使用 insert() 函数在集合中插入元素。此函数接受元素作为将插入到集合中的参数。cDg28资讯网——每日最新资讯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;}

输出:cDg28资讯网——每日最新资讯28at.com

Make Of Use

8. 从字符串中删除重复项

可以使用以下方法从字符串中删除重复字符:cDg28资讯网——每日最新资讯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;}

输出:cDg28资讯网——每日最新资讯28at.com

Original String:MakeUseOfNew String:MakeUsOf

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

您可以使用 length() 函数查找C++字符串的长度。或者,您也可以使用 size() 函数(它是长度() 函数的别名)。cDg28资讯网——每日最新资讯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;}

输出:cDg28资讯网——每日最新资讯28at.com

Length of MakeUseOf : 9Length of lorem ipsum : 11

10.从数组中删除元素

可以使用以下方法从数组中删除元素:cDg28资讯网——每日最新资讯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;}

输出:cDg28资讯网——每日最新资讯28at.com

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

有时,直接理解复杂的代码并不容易。您应该遵循一些基本的编程原则,如记录代码、重构等,以使代码更加健壮。cDg28资讯网——每日最新资讯28at.com

11. 迭代向量

您可以通过多种方式循环访问向量。以下是迭代向量的三种最常用的方法:cDg28资讯网——每日最新资讯28at.com

(1) 使用范围:cDg28资讯网——每日最新资讯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) 使用迭代器的引用:cDg28资讯网——每日最新资讯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;}

以上三个代码将显示相同的输出:cDg28资讯网——每日最新资讯28at.com

1 2 3 4 5

(3) 利用C++代码片段cDg28资讯网——每日最新资讯28at.com

利用这些C++代码片段来解决日常编程问题。无论您是使用C++编写简单程序还是竞争编程,这些代码片段都可以派上用场。cDg28资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-100336-0.html介绍 11 个常用的 C++ 代码

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: Python 五分钟学会五种定时大法

下一篇: 听说异步和解耦才是消息队列最有价值的功能

标签:
  • 热门焦点
  • 影音体验是真的强 简单聊聊iQOO Pad

    大公司的好处就是产品线丰富,非常细分化的东西也能给你做出来,例如早先我们看到了新的vivo Pad2,之后我们又在iQOO Neo8 Pro的发布会上看到了iQOO的首款平板产品iQOO Pad。虽
  • Redmi Buds 4开箱简评:才199还有降噪 可以无脑入

    在上个月举办的Redmi Note11T Pro系列新机发布会上,除了两款手机新品之外,Redmi还带来了两款TWS真无线蓝牙耳机产品,Redmi Buds 4和Redmi Buds 4 Pro,此前我们在Redmi Note11T
  • 2023年Q2用户偏好榜:12+256G版本成新主流

    3月份的性能榜、性价比榜和好评榜之后,就要轮到2023年的第二季度偏好榜了,上半年的新机潮已经过去,最明显的肯定就是大内存和存储的机型了,另外部分中端机也取消了屏幕塑料支架
  • 十个简单但很有用的Python装饰器

    装饰器(Decorators)是Python中一种强大而灵活的功能,用于修改或增强函数或类的行为。装饰器本质上是一个函数,它接受另一个函数或类作为参数,并返回一个新的函数或类。它们通常用
  • 在线图片编辑器,支持PSD解析、AI抠图等

    自从我上次分享一个人开发仿造稿定设计的图片编辑器到现在,不知不觉已过去一年时间了,期间我经历了裁员失业、面试找工作碰壁,寒冬下一直没有很好地履行计划.....这些就放在日
  • Python异步IO编程的进程/线程通信实现

    这篇文章再讲3种方式,同时讲4中进程间通信的方式一、 Python 中线程间通信的实现方式共享变量共享变量是多个线程可以共同访问的变量。在Python中,可以使用threading模块中的L
  • 零售大模型“干中学”,攀爬数字化珠峰

    文/侯煜编辑/cc来源/华尔街科技眼对于绝大多数登山爱好者而言,攀爬珠穆朗玛峰可谓终极目标。攀登珠峰的商业路线有两条,一是尼泊尔境内的南坡路线,一是中国境内的北坡路线。相
  • iQOO 11S新品发布会

    iQOO将在7月4日19:00举行新品发布会,推出杭州亚运会电竞赛事官方用机iQOO 11S。
  • 2299元起!iQOO Pad开启预售:性能最强天玑平板

    5月23日,iQOO如期举行了新品发布会,除了首发安卓最强旗舰处理器的iQOO Neo8系列新机外,还在发布会上推出了旗下首款平板电脑——iQOO Pad,其搭载了天玑
Top