C++ string类应用

C++类应用:深入理解String类 本文深入探讨了一个自定义String类的实现,包括构造函数、析构函数、复制构造函数、转换构造函数以及重载运算符的使用。通过实例和代码解析,展现了如何通过这些成员函数和运算符重载,使String类能够像内置类型一样方便地使用。 阅读详情

程序中许多时候都需要处理字符串,如果用C语言写一些方法函数,则效率比较低。如果是应用软件,可以考虑用string类,掌握string类常见的方法基本可以处理70%以上的关于字符串的问题了( 如果有必要的话,可以结合STL容器考虑)。当然如果自己直接写增删改查相关的处理函数也是可以的,下面直接上代码,将string常见操作都实现了一次,考试时可以作为参考!

#include <iostream>
#include <string>
using namespace std;


int main()
{
    //1.string类的构造函数
    string str1 = "aaaaa";//最简单的字符串初始化
    cout << str1 << endl; // aaaaa

    char *s = "bbbbb";
    string str2(s);//用c字符串s初始化
    cout << str2 << endl; // bbbbb

    char ch = 'c';
    string str3(5, ch);//用n个字符ch初始化
    cout << str3 << endl; // ccccc

    //2.string类的字符操作
    string str4 = "abcde";
    ch = str4[3];//operator[]返回当前字符串中第n个字符的位置
    cout << ch << endl; // d

    string str5 = "abcde";
    ch = str5.at(4);//at()返回当前字符串中第n个字符的位置,并且提供范围检查,当越界时会抛出异常!
    cout << ch << endl; // e

    //3.string的特性描述
    string str6 = "abcdefgh";
    int size = 0;
    size = str6.capacity();//返回当前容量
    cout << size << endl;  // 8
    size = str6.max_size();//返回string对象中可存放的最大字符串的长度
    cout << size << endl;  // 1073741820
    size = str6.size();//返回当前字符串的大小
    cout << size << endl; // 8
    size = str6.length();//返回当前字符串的长度
    cout << size << endl; // 8
    bool flag;
    flag = str6.empty();//判断当前字符串是否为空
    cout << flag << endl; // false,0
    int len = 15;
    str6.resize(len, 'x');//把字符串当前大小置为len,并用字符ch填充不足的部分
    cout << str6 << endl; // abcdefghxxxxxxx

    //4.string的赋值
    string str7;
    str7 = str6;//把字符串str6赋给当前字符串
    cout << str7 << endl; // abcdefghxxxxxxx
    str7.assign(str6);//把字符串str6赋给当前字符串
    cout << str7 << endl; // abcdefghxxxxxxx
    str7.assign(s);//用c类型字符串s赋值
    cout << str7 << endl; // bbbbb
    str7.assign(s, 2);//用c类型字符串s开始的n个字符赋值
    cout << str7 << endl; // bb
    str7.assign(len, 'x');//用len个字符ch赋值给当前字符串
    cout << str7 << endl; // xxxxxxxxxxxxxxx
    str7.assign(str6, 0, 3);//把字符串str7中从0开始的3个字符赋给当前字符串
    cout << str7 << endl; // abc
    string str8 = "0123456789";
    str7.assign(str8.begin(), str8.end());//把迭代器之间的字符赋给字符串
    cout << str7 << endl; // 0123456789

    //5.string的连接
    string str9 = "start";
    str9 += str8;//把字符串str9连接到当前字符串的结尾
    cout << str9 << endl; // start0123456789
    str9.append(s);//把c类型字符串s连接到当前字符串的结尾
    cout << str9 << endl; // start0123456789bbbbb
    str9.append(s, 2);//把c类型字符串s的前2个字符连接到当前字符串的结尾
    cout << str9 << endl; // start0123456789bbbbbbb
    str9.append(str8.begin(), str8.end());//把迭代器之间的一段字符连接到当前字符串的结尾
    cout << str9 << endl; // start0123456789bbbbbbb0123456789
    str9.push_back('k');//把一个字符连接到当前字符串的结尾
    cout << str9 << endl; //start0123456789bbbbbbb0123456789k

    //6.string的比较
    flag = (str8 == str9);//判断两个字符串是否相等
    cout << flag << endl; // 0
    flag = (str8 != str9);//判断两个字符串是否不相等
    cout << flag << endl; // 1
    flag = (str8 > str9);//判断两个字符串是否大于关系
    cout << flag << endl; // 0
    flag = (str8 < str9);//判断两个字符串是否为小于关系
    cout << flag << endl; // 1
    flag = (str8 >= str9);//判断两个字符串是否为大于等于关系
    cout << flag << endl; // 0
    flag = (str8 <= str9);//判断两个字符串否为小于等于关系
    cout << flag << endl; // 1

    //以下的3个函数同样适用于c类型的字符串,在compare函数中>时返回1,<时返回-1,=时返回0
    flag = str9.compare(str8);//比较两个字符串的大小,通过ASCII的相减得出!
    cout << flag << endl; // 1
    flag = str9.compare(6, 12, str8);//比较str9字符串从6开始的12个字符组成的字符串与str9的大小
    cout << flag << endl; // 1
    flag = str9.compare(6, 12, str8, 3, 5);//比较str9字符串从6开始的12个字符组成的字符串与str9字符串从3开始的5个字符组成的字符串的大小
    cout << flag << endl; // 1

    //7.string的字串
    string str10;
    str10 = str9.substr(10, 15);//返回从下标10开始的15个字符组成的字符串
    cout << str10 << endl; // 56789bbbbbbb012

    //8.string的交换
    str10.swap(str9);//交换str10与str9的值
    cout << str10 << endl; // start0123456789bbbbbbb0123456789k

    //9.string的查找,查找成功时返回所在位置,失败时返回string::npos的值,即是-1
    string str11 = "abcdefghijklmnopqrstuvwxyz";
    int pos;
    pos = str11.find('i', 0);//从位置0开始查找字符i在当前字符串的位置
    cout << pos << endl; //8
    pos = str11.find("ghijk", 0);//从位置0开始查找字符串“ghijk”在当前字符串的位置
    cout << pos << endl; // 6
    pos = str11.find("opqrstuvw", 0, 4);//从位置0开始查找字符串“opqrstuvw”前4个字符组成的字符串在当前字符串中的位置
    cout << pos << endl; // 14
    pos = str11.rfind('s', string::npos);//从字符串str11反向开始查找字符s在字符串中的位置
    cout << pos << endl; // 18
    pos = str11.rfind("klmn", string::npos);//从字符串str11反向开始查找字符串“klmn”在字符串中的位置
    cout << pos << endl; // 10
    pos = str11.rfind("opqrstuvw", string::npos, 3);//从string::pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置
    cout << pos << endl; // 14

    string str12 = "aaaabbbbccccdddeeefffggghhhiiijjjkkllmmmandjfaklsdfpopdtwptioczx";
    pos = str12.find_first_of('d', 0);//从位置0开始查找字符d在当前字符串第一次出现的位置
    cout << pos << endl; // 12
    pos = str12.find_first_of("eefff", 0);//从位置0开始查找字符串“eeefff“在当前字符串中第一次出现的位置
    cout << pos << endl; // 15
    pos = str12.find_first_of("efff", 0, 3);//从位置0开始查找当前串中第一个在字符串”efff“的前3个字符组成的数组里的字符的位置
    cout << pos << endl; // 15
    pos = str12.find_first_not_of('b', 0);//从当前串中查找第一个不在串s中的字符出现的位置
    cout << pos << endl; // 0
    pos = str12.find_first_not_of("abcdefghij", 0);//从当前串中查找第一个不在串s中的字符出现的位置
    cout << pos << endl; // 33
    pos = str12.find_first_not_of("abcdefghij", 0, 3);//从当前串中查找第一个不在由字符串”abcdefghij”的前3个字符所组成的字符串中的字符出现的位置
    cout << pos << endl; // 12
    //下面的last的格式和first的一致,只是它从后面检索!
    pos = str12.find_last_of('b', string::npos);
    cout << pos << endl; // 7
    pos = str12.find_last_of("abcdef", string::npos);
    cout << pos << endl; // 61
    pos = str12.find_last_of("abcdef", string::npos, 2);
    cout << pos << endl; // 45
    pos = str12.find_last_not_of('a', string::npos);
    cout << pos << endl; // 63
    pos = str12.find_last_not_of("abcdef", string::npos);
    cout << pos << endl; // 63
    pos = str12.find_last_not_of("abcdef", string::npos, 3);
    cout << pos << endl; // 63

    //10.string的替换
    string str13 = "abcdefghijklmn";
    str13.replace(0, 3, "qqqq");//删除从0开始的3个字符,然后在0处插入字符串“qqqq”
    cout << str13 << endl; // qqqqdefghijklmn
    str13.replace(0, 3, "vvvv", 2);//删除从0开始的3个字符,然后在0处插入字符串“vvvv”的前2个字符
    cout << str13 << endl; // vvqdefghijklmn
    str13.replace(0, 3, "opqrstuvw", 2, 4);//删除从0开始的3个字符,然后在0处插入字符串“opqrstuvw”从位置2开始的4个字符
    cout << str13 << endl; // qrstdefghijklmn
    str13.replace(0, 3, 8, 'c');//删除从0开始的3个字符,然后在0处插入8个字符 c
    cout << str13 << endl; // cccccccctdefghijklmn
    //上面的位置可以换为迭代器的位置,操作是一样的,在这里就不再重复了!

    //11.string的插入,下面的位置处亦可以用迭代器的指针表示,操作是一样的
    string str14 = "abcdefg";
    str14.insert(0, "mnop");//在字符串的0位置开始处,插入字符串“mnop”
    cout << str14 << endl; // mnopabcdefg
    str14.insert(0, 2, 'm');//在字符串的0位置开始处,插入2个字符m
    cout << str14 << endl; // mmmnopabcdefg
    str14.insert(0, "uvwxy", 3);//在字符串的0位置开始处,插入字符串“uvwxy”中的前3个字符
    cout << str14 << endl; // uvwmmmnopabcdefg
    str14.insert(0, "uvwxy", 1, 2);//在字符串的0位置开始处,插入从字符串“uvwxy”的1位置开始的2个字符
    cout << str14 << endl; // vwuvwmmmnopabcdefg

    //12.string的删除
    string str15 = "gfedcba";
    string::iterator it;
    it = str15.begin();
    it++;
    str15.erase(it);//删除it指向的字符,返回删除后迭代器的位置
    cout << str15 << endl; // gedcba
    str15.erase(it, it+3);//删除it和it+3之间的所有字符,返回删除后迭代器的位置
    cout << str15 << endl; // gba
    str15.erase(2);//删除从字符串位置3以后的所有字符,返回位置3前面的字符
    cout << str15 << endl; // gb
    
    string str("    abcabcssfabcsfsas    ");
    string::size_type pos(0);

    pos = str.find("abc", pos);
    while (pos != string::npos)
    {
         cout << str.substr(pos) << endl;//abcabcssfabcsfsas abcssfabcsfsas abcsfsas
         pos = str.find("abc", pos + 3);//查找所有abc的字符子串(包括abc本身)
    }

    pos = 0;
    pos = str.find("abc", pos);

    while (pos != string::npos)
    {
        str.replace(pos, 3, "");//将str所有的abc替换为空
        cout << str << endl;//abcssfabcsfsas ssfabcsfsas ssfsfsas
        pos = str.find("abc", pos);
    }

    pos = 0;
    pos = str.find_first_not_of(" ", pos);
    if (pos != string::npos)
    {
    	str.erase(0, pos); //去掉string前面空格
    	cout << str << endl;// "ssfsfsas    "
    }
    pos = 0;
    pos = str.find_last_not_of(" ", string::npos);
    if (pos != string::npos)
    {
        str.erase(pos+1); //去掉string后面空格
        cout << str << endl;// "ssfsfsas"
    }
    return 0;
}



点云从入门到精通技术详解100篇-基于三维点云的机器人抓取位姿检测(续) 集到的深度信息会存在噪点,因此需要将生成的场景点云进行预处理,获得平滑、降采样处理场景点云,本文设计的预处理操作更能从冗杂的场景点云中提取清晰、平滑的目标点云,便于后续的抓取位姿采样,提高后续算法的处理效率。因此需要对场景点云进行适当的分割,仅保留目标物体的点云,整个过程可以通过。场较小时,空间中的点云密度会更加提高;觉子系统的效率,再保持全局点云几何形状不改变的前提下,通过对目标点云进行。体素降采样可以快速地减少点云数据并同时保持点云的形。个点,由于我们实施抓取的目标物体只占整个场景点云图的一部分, 阅读详情

相关推荐

高通ISP模块全解析:从Raw到JPEG的18个关键处理步骤(附避坑指南)

本文全面解析高通ISP模块从Raw到JPEG的18个关键处理步骤,涵盖原始数据预处理、色彩科学处理链、画质增强与降噪等核心环节。通过实战调优经验,揭示ISP架构中的技术原理与工程实践要点,帮助开发者避免常见问题,提升移动影像处理质量。

weixin_30399155的博客 359

String详解

1 String 概述 2 String不可变的好处 3 String,StringBuffer和 StringBuilder的区别 4 字符串常量池(String Pool) 5 new String(String类中两种对象实例化的区别) 6 String 常用方法(字符串构造、String对象的比较 、字符串查找、转化、字符串替换、字符串拆分、字符串截取、其他操作方法)...

crazy_xieyi的博客 5728

UVM和System verilog笔记总结(四)

virtual sequence 即虚seq,用于管理多个实sequence的执行顺序。 下面有三个sequence,此时需要virtual sequence去管理这三个的顺序。 virtual sequence和virtual sequencer需要成对写,使用例化实的sequence和sequencer控制: 此时在virtual sequence里面启动,而不是之前在test.sv里面用default或start函数启动。具体方法如下:先例化 再启动,用 `uvm_do_on 实sequence

weixin_42655118的博客 642

C++中的String的常用函数用法总结

一. string的构造函数的形式: string str:生成空字符串 string s(str):生成字符串为str的复制品 string s(str, strbegin,strlen):将字符串str中从下标strbegin开始、长度为strlen的部分作为字符串初值 string s(cstr, char_len):以C_string类型cstr的前char_len个字...

qq_37941471的博客 46万+

OneNet平台MQTT连接避坑指南:C语言Token计算实战(附完整代码)

本文详细解析了OneNet平台MQTT连接中的Token计算机制,提供了C语言实现的完整代码和实战指南。通过HMAC算法和Base64编码,开发者可以高效生成安全凭证,避免常见连接问题,适用于物联网设备开发。

weixin_29193359的博客 460

Office Tool Plus:正版Office干净部署指南

Office部署是Windows新机配置的关键环节,涉及版本选择、架构匹配、语言支持与合规激活等基础技术问题。其核心原理基于微软官方Office Deployment Tool(ODT)的声明式XML配置机制,通过标准化流程确保安装可复现、可审计、无第三方捆绑。技术价值在于规避官网安装器的版本锁定、架构误配和语言缺失等常见风险,提升部署稳定性与安全性。典型应用场景包括企业批量部署、IT运维标准化、隐私敏感型用户自主安装。本文以Office Tool Plus为实践载体,深入解析ODT底层逻辑、XML配置规范

1181

String str

String str=new String(“abc”); 跟着这段代码之后,我们会想到一个问题,就是这行代码究竟创建了几个String对象呢?答案是2个。String str只是定义了一个名为str的String类型的变量,因此它并没有创建对象;=是对变量str进行初始化,将某个对象的引用(或者叫句柄)赋值给它,显然也没有创建对象;现在只剩下了new String...

kongzheng1993的博客 1126

Simulink实例--电动汽车充电站与微电网协同运行仿真

​电网负荷剧增:单站1000台车同时充电需150MW功率(超配电网容量30%)​能源孤岛问题:传统充电站仅作为单向负荷,无法参与电网调频​可再生能源消纳:光伏/风电波动导致充电站功率波动±40%​多目标矛盾:充电效率、电网稳定性、设备寿命的协同优化​节能效果:每百万辆车年节电1.8GWh​成本回收:投资回报周期<4年(IRR>25%)​减排效益:每年减少CO₂排放12万吨。

xiaoheshang_123的博客 330

String类详解

String类算是比较特殊的类型了,作为初学者我们甚至把这个引用类型时不时当成基本类型来用,而且发现,竟然差不多。然而实际呢?我们太年轻,差的真的很多,今天就来详细讲一讲到底差在哪。 一、创建字符串的四种方式 1.直接赋值 直接赋值,在堆中的常量池中创建该字符串对象 String str1 = "HelloWorld"; 2.通过构造方法创建对象 在堆中创建对象,并把该对象的拷贝放到常量池中 String str2 = new String("HelloWorld"); 3.通过字符数组

weixin_65278827的博客 2万+

vue3+vite - 解决报错error while updating dependencies:Error:EACCES:permission denied,mkdir ‘x‘(系统权限问题)

vue3,vite,报错,nuxt,权限,苹果mac系统,windows系统,vue3报错,vite报错,node,详细解决方法,教程,[vite] error while updating dependencies: Error:EACCES:permission denied,mkdir 'x',更新依赖项时发生[vite]错误:错误:EACCES:权限被拒绝,mkdir,苹果mac系统加了sudo还是不行报错,运行报错,启动项目,run dev,创建项目,初始化,依旧不行怎么办,保权限没有问题,换了n

✅ 精选推荐文章 5555

智联招聘Python岗位数据采集:基于API请求的校园招聘信息爬取实战

本项目开发了一个智联招聘Python岗位校园招聘数据采集系统,采用requests库直接调用API接口获取结构化数据。系统通过分析API接口结构,配置完整的请求头、Cookie和身份验证参数,实现了10页数据的批量采集。采集内容包括工作名称、薪资范围、企业信息等10个关键字段,并存储为Excel格式。项目严格遵守网络爬虫规范,合理控制请求频率,数据仅用于技术学习和就业分析。技术实现涉及API逆向工程、JSON数据处理、防御性编程等核心技能,为学生职业规划和教育机构教学改革提供数据支持。

那些看似不起波澜的日复一日,会在某天让你看到坚持的意义 626

String类的学习笔记(上):介绍String类及其常用方法的使用

本文介绍了Java中用来描述操作字符串的String类,和其一些常用的基本操作方法,字符串的创建输出,字符串对象的比较,字符串查找,字符串的转化,字符串的替换,字符串拆分,字符串截取,和大小写转换,去除左右空格,子字符串包含,学会使用这些方法,能更方便的使用操作字符串~

记录并分享学习编程时的知识点... 4036

SpringBoot项目搭建

SpringBoot项目搭建

私信可回答各种问题~ 1020

【C++】 string类:应用与实践

①使用getline函数(后文有解析)获取一行字符串;②使用rfind函数(使用方法在后文)从字符串末尾往前找到第一个空格,返回该空格的位置;③使用size函数得到整个字符串长度,减去之前空格的位置再-1,得出最后一个单词的长度。//获取一行字符串//从后往前遍历找到空格的位置//计算最后一个单词长度//打印长度🥳🥳getline使用方法getline是C++中用于从输入流中读取一行文本的函数。

Renswc的博客 2504

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

C++:string类的应用

string类的应用,在线OJ笔试题,深浅拷贝(重点),写时拷贝

碧瑶的博客呀 323

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

C++初阶-string类的简单应用

这个题目其实我想在讲getline函数时就讲了,这个就是getline函数和rfind函数(我自己差点也忘记这个函数了)的应用了!//a.size()-1是真实长度//而再减去剩余的长度即真实长度//因为rfind是返回找到的元素的下标(若为字符串则返回最前的一个元素下标)这讲的题目比较经典,所以提前讲了,之后在把string类手动实现后我将讲解一些扩展的string题目,下节将讲解:string类的手动实现。喜欢的可以一键三连哦,下讲再见!

博客是在C++/Java/Liunx/算法的学习阶段所写的博客,内容可能不是很全面 1057

C++ 中的 string 类:全面解析与应用实践

编程中字符串至关重要。无论是构建用户界面、处理数据还是网络通信,都离不开字符串操作。C 语言里的数组处理字符串时,需手动管理内存,容易出现缓冲区溢出等错误,像字符串拼接、复制等常见操作,代码编写繁琐且易出错。C++ 的类则是强大的字符串处理工具。它能自动处理内存分配与释放,避免手动管理的风险。还提供大量实用成员函数,如拼接、查找、替换、比较等操作都很便捷,可有效提升开发效率,助力开发者专注核心业务逻辑,在 C++ 编程里处理字符串极为得力

jsjs1346的博客 1507

(C++基础_STL) —— string类的基本应用

文章目录(C++基础_STL) —— string类的基本应用0 个人介绍string容器1. string的基本概念2. string的构造函数案例3. string赋值操作4. string的字符串拼接操作5. string的查找和替换6. (C++基础_STL) —— string类的基本应用      在日常的C++开发过程中,我们会经常使用到STL最为赋能加速开发的一个基本库,那么在有一定的C++的语言的基础上,学习如何使用STL是一件非常有益与后续有益于自己面对更加复杂的需求和日常开发赋能的

莫寒技术人生 318

C++ STL string类全面指南:从编码历史到实战应用

例如,std::wstring用于宽字符(通常为wchar_t,在Windows平台中一般为2字节,在Linux平台中通常为4字节),以及C++11标准引入的std::u16string和std::u32string,分别用于处理UTF-16和UTF-32编码的字符,对应的字符类型为char16_t(2字节)和char32_t(4字节)string在后续的修改也参考了STL的设计,但是string在使用的功能和设计的风格来说,它和容器几乎一样,所以后面把string归到容器。可以看到string不在。

2509_93923260的博客 357

【C++STL :string类 (二) 】从接口应用到内存模型的全面探索

加油!!!

艾莉丝努力练剑的博客,感谢大家一直以来的对艾莉丝的支持和鼓励,uu们如果对C/C++方向感兴趣的话,可以通过艾莉丝主页的微信名片加艾莉丝的好友,和努力练剑的艾莉丝一起加油! 1458

c++ string 类的应用

string 类作为一个模版类,在使用的时候有很多方便的地方。 特别是在内存使用上面,在里面做了动态的分配,这一点和vector很像,以后会更多的总结。 以下为string的文章,后续会做总结。 https://www.cnblogs.com/tongye/archive/2019/04/24/10760154.html ......

linan101的博客 150

C++:String类应用

C++:String的应用

qq_50426849的博客 740

C++ string类详细讲解:从原理讲解到实战应用

本文章主要讲述了string的基本成员函数的使用,迭代器以及auto和范围for的使用

Jammy_1的博客 256

详解!! C++ String类应用及常见接口整合

1. String类 string是表示字符串的字符串类 该类的接口与常规容器的接口基本相同,再添加一些专门用来操作string的常规操作。 String在底层实际是:basic_string模版类的别名,typedef basic_string&amp;lt;char,char_traits,allocator&amp;gt;string; 不能操作多字节或者变长字符的序列。 在使用string类的时候,必须...

qq_41906009的博客 339

C++ String类深度探索:从基础到高级应用的全方位指南

在C++编程世界中,string类无疑是处理文本数据的基石。作为标准模板库(STL)的一部分,std::string提供了丰富而强大的功能,使得字符串的创建、操作和管理变得既高效又便捷。从简单的字符串拼接、查找、替换,到复杂的字符串格式化、转换和内存管理,std::string类都为我们提供了完善的解决方案。本文旨在深入探讨C++中string类的核心特性、常用成员函数及其背后的实现原理,并通过实例演示如何在实际编程中高效地使用string类。

17的博客 2263
上一篇: 表驱动法在项目中的应用
下一篇: Redis双向链表浅析
CodingGeek
博客等级 码龄18年 15粉丝 31原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值