迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中使用 void 函数

作者:迹忆客 最近更新:2023/03/31 浏览次数:

本文将演示有关如何在 C++ 中使用 void 函数的多种方法。


使用 void 函数查找更长的字符串

没有返回值的函数将 void 类型指定为返回参数。在 void 函数中,隐式的 return 语句在函数体的最后一条语句之后调用。注意,可以将显式的 return 语句放置在 void 函数主体中,在该主体中,控制流需要立即移至调用方函数。在下面的示例中,isLessString 函数包含将条件字符串打印到控制台的唯一条件语句,并在其后执行隐式 return

#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>

using std::cout; using std::endl;
using std::string; using std::map;
using std::vector;

void isLessString(string &s1, string &s2)
{
    s1.size() < s2.size() ?
    cout << "string_1 is shorter than string_2" << endl:
    cout << "string_2 is shorter than string_1" << endl;
}

int main() {
    string str1 = "This string has arbitrary contents";
    string str2 = "Another string with random contents";

    isLessString(str1, str2);

    return EXIT_SUCCESS;
}

输出:

string_1 is shorter than string_2

使用 void 函数查找 map 中是否存在键

在这种情况下,void 函数可用于实现 std::map 容器的关键字搜索功能。注意,通过将相应的字符串常量打印到 cout 流来传达结果。尽管这不是在程序内部传递数据的首选方法,但可将其用于指示信息给最终用户。

#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>

using std::cout; using std::endl;
using std::string; using std::map;
using std::vector;

void keyExistsInMap(map<string, string> &m, const string& key)
{
    m.find(key) != m.end() ?
    cout << "Key exists in a map" << endl :
    cout << "Key does not exist in a map" << endl;
}

int main() {
    map<string, string> veggy_map = {{"a", "Ali",},
                                     {"m", "Malvo",},
                                     {"p", "Pontiac",},
                                     {"s", "Sensi",}};

    keyExistsInMap(veggy_map, "s");
    keyExistsInMap(veggy_map, "x");

    return EXIT_SUCCESS;
}

输出:

Key exists in a map
Key does not exist in a map

使用 void 函数对向量中的元素进行排序

或者,可以为 std::sort 算法实现包装函数,该包装函数以 vector 对象为参考,并以升序对元素进行排序。请注意,void 函数通常缺乏将故障传达给调用者函数的方法,因此在设计解决方案时应始终考虑这一点。

#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>

using std::cout; using std::endl;
using std::string; using std::map;
using std::vector;

void sortVector(vector<string> &vec)
{
    std::sort(vec.begin(), vec.end(),
              [] (const auto &x, const auto &y) {return x < y;});
}

int main() {
    vector<string> arr = { "element", "implementation", "or", "the",
                           "execution", "dependent", "template", "character",
                           "that", "all", "via", "class" };

    sortVector(arr);

    for (const auto &item : arr) {
        cout << item << "; ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

输出:

all; character; class; dependent; element; execution; implementation; or; template; that; the; via;

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便