54>>c++ string

string类的定义、操作。

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

int main()
{
	// 4 declare
	string s1;			//default
	string s2( s1 );
	string s3( "hello" );
	string s4( 9, ‘s‘ );

	//read and write
	string s_1, s_2;
	cin>>s_1>>s_2;			//igore blank in head, find blank in tail will end;
	cout<<s_1<<endl;

	//读入未知数目的文本
	string word;
	while( cin>>word )		//read until end-of -file
	{
		cout<<word<<endl;
		if( word == "exit" )
		{
			cout<<"exit success"<<endl;
			break;
		}
	}

	//用getline读取整行文本,getline不会保存换行符
	string line;
	while( getline( cin, line ) )
	{
		cout<<line<<endl;
		if( line == "exit" )
		{
			cout<<"exit success"<<endl;
			break;
		}
	}

	//string.size() and string.empty
	string str_size;
	cout<<"test string size"<<endl;
	cin>>str_size;
	if( str_size.empty() )
		cout<<"string is empty"<<endl;
	else
		cout<<"string is not empty"<<endl;

	//配套类型string::size_type实现与及其无关;int 型变量容易出错,不便移植;unsigned 可用于string::size_type可用的地方。
	//关系操作符==、+=、<、<=、>、>=,使用字典顺序排序
	string big = "big", small = "small", substr = "hello",phrase = "hello world";		//substr 小于 phrase

	//string赋值,有效率上的问题
	string str1, str2 = "value assignment come with efficent problem";
	str1 = str2;

	//string对象相加
	str2 = "c plus plus";
	str1 = "hell";
	str2 += str1;

	//和字符串字面值的连接,必须用string类型作为左值
	str1 = str2 + "hello world" + "!!!";

	//下标操作string单个字符
	for( string::size_type ix = 0; ix != str1.size(); ix++ )
	{
		cout<< str1[ix] <<endl;
		str1[ix] = ‘*‘;
	}

	//下标值计算
	str1[2*1] = ‘a‘;
	cout<<str1<<endl<<"下标计算"<<endl;

	//string中单个字符处理
	cout<<"单个字符的处理"<<endl;
	if( ispunct(str1[2]) )
		cout<<"str1[2] 是一个标点"<<endl;

	string s;
	cout<<s[0]<<endl;		//越界

	return 0;
}

操作单个字符时使用cctype定义的函数,满足条件则为true:

isalnum(c)  是字母或数字

isalpha(c)  是字母

iscntrl(c)  是控制字符

isdigit(c)  是数字

isgraph(c)  不是空格,可打印

islower(c)  小写字母

isprint(c)  可打印字符

ispunct(c)  标点符号

isspace(c)  空白字符

isupper(c)  大写字母

isxdigit(c)  十六进制数

tolower(c)  是大写字母时返回小写字母,否则返回c

toupper(c)  是小写字母时返回大写字母,否则返回c

时间: 2024-08-24 01:30:55

54>>c++ string的相关文章

LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)

Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext. The given compressed string will be in the form of each letter followed by a positive integer representing the numbe

String类简单实现

原文地址:http://rsljdkt.iteye.com/blog/774188 1 #include "stdafx.h" 2 #include "stdlib.h" 3 #include<iostream> 4 using namespace std; 5 6 class String 7 { 8 public: 9 String(const char *str = NULL);//默认构造函数 10 String(const String &am

Java基础笔记-String类

String 类(被final修饰) 字符串是一种特殊的对象,一旦字符串被初始化就不可以被改变了.(内容不变) 例如: String  s = “abc”; String  s1 = new String(“abc”); s在内存中有一个对象, s代表的是一个类类型变量,”abc”是一个对象. s1在内存中有两个对象,分别是new出来的和:  “abc” . s == s1;的结果是false.   因为s和s1它们所对应的地址不同,比较的两个地址,s和s1中存储的地址数值是不同的.因此是fal

JS基础_强制类型转换-String

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 <script type="text/javascript"> 7 8 /* 9 * 强制类型转换 10 * - 指将一个数据类型强制转换为其他的数据类型 11 * - 类型转换主要指,将其他的数据类型,转换为 12 * S

微信JS-SDK实现上传图片功能

最近在项目开放中,有一个在微信WEB项目中上传图片的需求,一开始使用了传统的<input type="file">的方式去实现,但是后面发现在使用这种传统模式时会由于手机系统的差异而导致一系列的问题,后改用微信JSSDK的方式来实现. 总的来说,利用JSSDK来实现该功能一共分为四步. 1. 调用wx.config(),初始化jssdk的配置,并在jsApiList中配置上传图片需要的四个api('chooseImage','previewImage','uploadIma

帮助类4

#region 构造URL GET请求 2 /// <summary> 3 /// 获取请求的反馈信息 4 /// </summary> 5 /// <param name="url">地址</param> 6 /// <returns></returns> 7 public static string doGetRequest(string url) 8 { 9 HttpWebRequest hwRequest;

WCF实现客户端自动更新-GenerateFileList

GenerateFileList 1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.IO; 5 6 namespace GenerateFileList 7 { 8 public class Generator 9 { 10 private readonly string _appPath = AppDomain.CurrentDomain.BaseDire

Spark入门之WordCount详细版

1 package cn.spark.study.core; 2 3 import java.util.Arrays; 4 5 import org.apache.spark.SparkConf; 6 import org.apache.spark.api.java.JavaPairRDD; 7 import org.apache.spark.api.java.JavaRDD; 8 import org.apache.spark.api.java.JavaSparkContext; 9 impo

类与对象

1.1 类和对象的关系   为什么要采用面向对象思想进行程序设计与开发 1.可以实现代码的复用 2.符合人思考的方式   类和对象的定义 1.类的定义:用class关键修饰的叫做类 2.对象的定义:类名定义的数据类型   类和对象之间关系 1.由对象归纳为类,是归纳对象共性的过程 2.在类的基础上,将状态和行为实体化为对象的过程为实例化   1.2 定义类   定义类的语法,类主要由成员变量和成员方法构成(暂不提构造函数) eg: publicclassStudent { //成员变量 intn