c++++ 自身函数示例:字符串操作:std::string::find 函数查找子串。数值转换:std::stoi 和 std::stof 函数分别将字符串转换为整数和浮点数。容器操作:std::vector::push_back 函数添加元素,std::sort 函数对元素排序。输入/输出:std::cin 和 std::cout 分别从标准输入读取和输出数据。时间操作:std::time 函数获取当前时间戳,std::localtime 函数将其转换为本地时间。
C++ 自身函数应用举例
C++ 提供了丰富的自身函数,用于处理各种数据类型和操作,本文以几个实战案例展示其应用。
1. 字符串操作
立即学习“C++免费学习笔记(深入)”;
string::find 函数用于查找字符串中特定子串的位置,例如:
#include int main() { std::string myString = "Hello, world!"; size_t pos = myString.find("world"); if (pos != std::string::npos) { std::cout2. 数值转换
std::stoi 函数将字符串转换为整数,std::stof 函数将字符串转换为浮点数,例如:
#include #include int main() { std::string myString = "123.45"; int myInt = std::stoi(myString); float myFloat = std::stof(myString); std::cout3. 容器操作
std::vector::push_back 函数将元素追加到向量末尾,std::sort 函数对容器中的元素进行排序,例如:
#include #include int main() { std::vector myVector; myVector.push_back(1); myVector.push_back(3); myVector.push_back(2); std::sort(myVector.begin(), myVector.end()); for (int element : myVector) { std::cout4. 输入/输出
std::cin 函数从标准输入读取数据,std::cout 函数输出数据到标准输出,例如:
#include int main() { int number; std::cout > number; std::cout5. 时间操作
std::time 函数返回当前时间自纪元以来经过的秒数,std::localtime 函数将其转换为本地时间,例如:
#include #include int main() { std::time_t currentTime = std::time(nullptr); std::tm *localTime = std::localtime(¤tTime); std::cout tm_hour tm_min tm_sec登录后复制
以上就是C++ 自身函数应用举例的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2453925.html