日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

stl取出字符串中的字符_从C ++ STL中的字符串访问字符元素

發布時間:2023/12/1 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 stl取出字符串中的字符_从C ++ STL中的字符串访问字符元素 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

stl取出字符串中的字符

字符串作為數據類型 (String as datatype)

In C, we know string basically a character array terminated by \0. Thus to operate with the string we define character array. But in C++, the standard library gives us the facility to use the string as a basic data type like an integer. But the string characters can be still accessed considering the string as a character array.

在C語言中,我們知道字符串基本上是一個以\ 0結尾的字符數組。 因此,要對字符串進行操作,我們定義了字符數組。 但是在C ++中,標準庫為我們提供了將字符串用作基本數據類型(如整數)的便利。 但是,仍可以將字符串視為字符數組來訪問字符串字符。

Example:

例:

Like we define and declare,string s="IncludeHelp";s[0] = 'I' (not "I")s[7] = 'H' //So same way we can access the string element which is character.//Also there is a function under string class, at(), //which can be used for the same purpose.cout << s.at(7); //prints H

Remember, a string variable (literal) need to be defined under "". 'a' is a character whereas "a" is a string.

請記住,需要在“”下定義一個字符串變量(文字)。 “ a”是字符,而“ a”是字符串。

Note: Trying to accessing character beyond string length results in segmentation fault.

注意:嘗試訪問超出字符串長度的字符會導致分段錯誤。

Header file needed:

所需的頭文件:

#include <string>Or#include <bits/stdc++.h>

C++ program to demonstrate example of accessing characters of a string

C ++程序演示訪問字符串字符的示例

#include <bits/stdc++.h> using namespace std;int main(){string s;cout<<"enter string...\n";cin>>s;cout<<"Printing all character elements...\n";for(int i=0;i<s.length();i++)cout<<s[i]<<" ";return 0; }

Output

輸出量

enter string... IncludeHelp Printing all character elements... I n c l u d e H e l p

翻譯自: https://www.includehelp.com/stl/accessing-character-elements-from-a-string-in-cpp-stl.aspx

stl取出字符串中的字符

總結

以上是生活随笔為你收集整理的stl取出字符串中的字符_从C ++ STL中的字符串访问字符元素的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。