1. 程式人生 > >C++string的使用

C++string的使用

調庫:#include<string>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
	string str="aabcdefg";
	string str1="hijklmn";
	const char *p="opqrst";
	string str2=p;
	string str4;
	int i;
	
	//用陣列的方法遍歷str
	for(i=0;i<str.length();i++)
		printf("%c",str[i]);
	cout<<endl;
	
	//迭代器遍歷str,類似於指標
	string::iterator it;
	for(it=str.begin();it!=str.end();it++)
		printf("%c",*it);
	cout<<endl;
	
	//查詢字串的位置
	cout<<str.find("a",0)<<endl;//從開始的位置開始查詢a的位置(第一次出現的位置)
	cout<<str.find("bcd",0)<<endl;
	cout<<str.find("cdf",0,2)<<endl;  //從開始查詢子串前兩個字元在整個串中第一次出現的位置
	
	//擷取字串
	string str3=str.substr(0,4);  //以0下標為開始,向後截4個字元給str3,假如目標串不夠這些字元,則最多就只有這些字元
	cout<<str3<<endl;

	//字串的連線
	string str5;
	str5=str+str1;
	cout<<str5<<endl;
	
	//字串的插入
	cout<<str.insert(1,str1)<<endl;  //在str串中的下標為1的位置插入一個str1,返回插入好的結果串

	//兩個串交換
	str.swap(str1);
	cout<<str<<endl;
	cout<<str1<<endl;
	return 0;
}

相關推薦

利用C++ string實現任意長度小數、整數之間的加法

code include spa ctype empty esp pac space temp 1 /* 2 大數的運算1--加法: 3 利用C++ string實現任意長度小數、整數之間的加法 4 作者:大大維 5 2017/5/5

C++string中用於查找的find系列函數淺析

ace 規則 ret stream num 原因 目標 自己 查找字符 總述: 以下所講的所有的string查找函數,都有唯一的返回類型,那就是size_type,即一個無符號整數(按打印出來的算)。若查找成功,返回按查找規則找到的第一個字符或子串的位置;若查找失

C String理解—— 字符串替換函數

rcp wstring cnblogs repl 指定 bcd str right stdio.h 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4

C++ string中的幾個小陷阱,你掉進過嗎?

stl 試題 賦值 clu ror ati world mod iostream C++開發的項目難免會用到STL的string。使用管理都比char數組(指針)方便的多。但在得心應手的使用過程中也要警惕幾個小陷阱。避免我們項目出bug卻遲遲找不到原因。1. 結構體中的

c++ string類的完整實現!!!

class sun double ref basic 更新 方便 iterator locate 本文實現了c++ STL中的basic_string模板類,當然。通過typedef也就實現了string類和wstring類。限於篇幅,實現代碼中用到了標準庫的char_

C++string類用法

last emp 進行 iii str2 line rep har ddd 1.string類的輸入操作(string類重載運算符operator>>用於輸入,同樣重載運算符operator<<用於輸出操作)operator是C++的關鍵字,它和運算

c++ --string操作

col 匹配 borde append width 開始 不支持 wid font 順序容器--額外的string操作   除了順序容器共同的操作之外,string類型還提供了一些額外的操作。這些操作中大致可以分為兩部分:   1. 提供string類和C風格字符出租之

Codeforces C - String Reconstruction

pre div als log ++ codeforce tdi 父親 span C - String Reconstruction 方法一:把確定的點的父親節點設為下一個點,這樣訪問過的點的根節點都是沒訪問過的點。 代碼: #include<bits/stdc++

Codeforces Round #423 Div. 2 C-String Reconstruction(思維)

images memset clu 技術 ret .cn har ges round 題目大意:告訴你n個字符串以及這些字符串在字符串s中出現的位置(x1,x2.....xn),要求在滿足上述條件的情況下,求出字典序最小的字符串s。 解題思路:主要問題是,如果直接模擬是會超

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction

題意 一個 () max 字典序 log class its 位置 題意:給出各個字符串出現的起始位置,問整個的字符串是什麽,(字典序最小) 思路:開始寫的是用set+優先隊列存取每個位置出現的最長字符串,然後遍歷,爆內存。。。爆。。。內。。。存。。。我們可以用並查集,已經

搜集的 C#:String.Format數字格式化輸出

one 描述 enter 自定義 int padding 允許 如果 margin 今天需要用到 大額的金額數字轉換 ,上網搜了一下有人整理好了 於是轉載過來 ,備用 格式化處理大額度金額: String.Format("{0:N}", "628310.03") 628,3

Round #423 C. String Reconstruction(Div.2)

sum turn include truct urn tput aaa eas tin Ivan had string s consisting of small English letters. However, his friend Julia decide

C# String與Byte數組的轉換

pan style clas -h 數組 ets div system logs string轉byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes(str); byte[] byteArray

c# string

prototype string對象 service names tr1 連續 pin lin ace string最為顯著的一個特點就是它具有恒定不變性:我們一旦創建了一個string,在managed heap 上為他分配了一塊連續的內存空間,我們將不能以任何方式對這個

c++ string

新的 內存 一個 分配內存 生命周期 回收 對象 釋放內存 返回 1 string對象的內存的分配和回收 如果string很短,那麽內存是在棧上面分配的。如果string很長的話,在堆上分配內存。 string生命周期結束的話,會自動調用string的析構函數釋放內存。 2

C++ string註意事項

工程 rate pac sin 命名空間 mes car 空間 時有 1:作為一個容器String是十分重要而常見的; 首先:#include <string> 使用前,然後在命名空間中使用加空間名;刷題時有事直接打using namespace std::st

c++String

... getchar() 子串 ins ted 類型 num ron .com C++ 標準庫提供了 string 類類型,支持上述所有的操作,另外還增加了其他更多的功能。我們將學習 C++ 標準庫中的這個類,現在讓我們先來看看下面這個實例: 現在您可能還無法透徹地理解這

C# String.IsNullOrEmpty Javascript equivalent

AC HR alert str java log OS rem target https://stackoverflow.com/questions/5746947/c-sharp-string-isnullorempty-javascript-equivalent

C++string類總結

signed align 初學 獲取 amp hello tail 即將 -h 一、string的初始化 首先,為了在程序中使用string類型,必須包含頭文件 <string>。如下: #include <string> 註意這裏不是st

代寫C++程序作業 代寫C ++String Vector

erl Go stat corn 網站開發 網絡安全 CP sta provides 代寫C++程序作業 代寫C ++String Vector#LAB 02- String Vector **Any program that do not compile will rec