c++学习随笔
1.strlen 用于取字符串长度,需要头文件 cstring
size_t strlen(const char *string);
头文件:string.h
格式:strlen (字符数组名)
功能:计算字符串s的(unsigned int型)长度,不包括'\0'在内
说明:返回s的长度,不包括结束符NULL。
2.gets() 用于接受连续的输入,包括空格,TAB
char * gets ( char * str );
3.gets,cin.get 的读取以程序运行开始
int a;cin>>a;
char str[1000];
for(int i=1;i<=a;i++)
{
gets(str); //直接触发第一次
for(int j=0;j<strlen(str);j++)
cout<<str[j];
}
4. 以 EOF(End of file) 作为输入结尾
while(cin>>s)
{
cout<<s<<"\n\n";
}
cin 是 C ++ 的标准输入流,其本身是一个对象,并不存在返回值的概念。
不过经常会有类似于
while(cin>>a)
的调用,这里并不是 cin 的返回值,而是 >> 操作重载函数
istream& operator>>(istream&, T &);
的返回值,其中第二个参数由 cin>> 后续参数类型决定。
其返回值类型为 istream& 类型,大多数情况下其返回值为 cin 本身(非 0 值),只有当遇到 EOF 输入时,返回值为 0。
5.string 用法总结
1. 长度
length 成员函数返回字符串的长度。size 成员函数可以实现同样的功能。
string a;
cout<<a.length();
2. 字符串的连接
除了可以使用 + 和 += 运算符对 string 对象执行字符串的连接操作外,string 类还有 append 成员函数,可以用来向字符串后面添加内容。append 成员函数返回对象自身的引用。例如:
string s1("123"), s2("abc");
s1.append(s2); // s1 = "123abc"
s1.append(s2, 1, 2); // s1 = "123abcbc"
s1.append(3, 'K'); // s1 = "123abcbcKKK"
s1.append("ABCDE", 2, 3); // s1 = "123abcbcKKKCDE",添加 "ABCDE" 的子串(2, 3)
3. 求 string 对象的子串
substr 成员函数可以用于求子串 (n, m),原型如下:
string substr(int n = 0, int m = string::npos) const;
调用时,如果省略 m 或 m 超过了字符串的长度,则求出来的子串就是从下标 n 开始一直到字符串结束的部分。例如:
string s1 = "this is ok";
string s2 = s1.substr(2, 4); // s2 = "is i"
s2 = s1.substr(2); // s2 = "is is ok"
4. 查找子串和字符
- find:从前往后查找子串或字符出现的位置。
- rfind:从后往前查找子串或字符出现的位置。
- find_first_of:从前往后查找何处出现另一个字符串中包含的字符。例如:
- s1.find_first_of("abc"); // 查找 s1 中第一次出现 "abc" 中任一字符的位置
- find_last_of:从后往前查找何处出现另一个字符串中包含的字符。
- find_first_not_of:从前往后查找何处出现另一个字符串中没有包含的字符。
- find_last_not_of:从后往前查找何处出现另一个字符串中没有包含的字符。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("Source Code");
int n;
if ((n = s1.find('u')) != string::npos) //查找 u 出现的位置
cout << "1) " << n << "," << s1.substr(n) << endl;
//输出 l)2,urce Code
if ((n = s1.find("Source", 3)) == string::npos)
//从下标3开始查找"Source",找不到
cout << "2) " << "Not Found" << endl; //输出 2) Not Found
if ((n = s1.find("Co")) != string::npos)
//查找子串"Co"。能找到,返回"Co"的位置
cout << "3) " << n << ", " << s1.substr(n) << endl;
//输出 3) 7, Code
if ((n = s1.find_first_of("ceo")) != string::npos)
//查找第一次出现或 'c'、'e'或'o'的位置
cout << "4) " << n << ", " << s1.substr(n) << endl;
//输出 4) l, ource Code
if ((n = s1.find_last_of('e')) != string::npos)
//查找最后一个 'e' 的位置
cout << "5) " << n << ", " << s1.substr(n) << endl; //输出 5) 10, e
if ((n = s1.find_first_not_of("eou", 1)) != string::npos)
//从下标1开始查找第一次出现非 'e'、'o' 或 'u' 字符的位置
cout << "6) " << n << ", " << s1.substr(n) << endl;
//输出 6) 3, rce Code
return 0;
}
5. 替换子串
replace 成员函数可以对 string 对象中的子串进行替换,返回值为对象自身的引用。例如:
string s1("Real Steel");
s1.replace(1, 3, "123456", 2, 4); //用 "123456" 的子串(2,4) 替换 s1 的子串(1,3)
cout << s1 << endl; //输出 R3456 Steel
string s2("Harry Potter");
s2.replace(2, 3, 5, '0'); //用 5 个 '0' 替换子串(2,3)
cout << s2 << endl; //输出 HaOOOOO Potter
int n = s2.find("OOOOO"); //查找子串 "00000" 的位置,n=2
s2.replace(n, 5, "XXX"); //将子串(n,5)替换为"XXX"
cout << s2 < < endl; //输出 HaXXX Potter
6. 删除子串
erase 成员函数可以删除 string 对象中的子串,返回值为对象自身的引用。例如:
string s1("Real Steel");
s1.erase(1, 3); //删除子串(1, 3),此后 s1 = "R Steel"
s1.erase(5); //删除下标5及其后面的所有字符,此后 s1 = "R Ste"
7. 插入字符串
insert 成员函数可以在 string 对象中插入另一个字符串,返回值为对象自身的引用。例如:
string s1("Limitless"), s2("00");
s1.insert(2, "123"); //在下标 2 处插入字符串"123",s1 = "Li123mitless"
s1.insert(3, s2); //在下标 2 处插入 s2 , s1 = "Li10023mitless"
s1.insert(3, 5, 'X'); //在下标 3 处插入 5 个 'X',s1 = "Li1XXXXX0023mitless"
8. 获取字符串元素
字符串中元素的访问是允许的,一般可使用两种方法访问字符串中的单一字符:下标操作符[] 和 成员函数 at()。两者均返回指定的下标位置的字符。第 1 个字符索引(下标)为 0,最后的字符索引为 length()-1。
9. 将字符串转换为整数
int stoi(const string&str,size_t * idx = 0,int base = 10);
int stoi(const wstring&str,size_t * idx = 0,int base = 10);
idx 为起始位置,base 为字符进制。
需 #include<string>,且为 c ++11 开始增加的新 api
DEV 设置方法:工具 -> 编译器选项 -> 代码生成 / 优化 -> 代码生成 -> 语言标准 (-std)
(Tools -> Compiler Options -> Setting -> Code Generation -> Language standard(-std))
10. 获取整行
在含有空格,TAB 的输入中需要用到 getline()
getline(cin,str);
11. 反转字符 reverse
反转范围中元素的顺序 [first,last),函数调用 iter_swap 将元素交换到新位置,该功能模板的行为等效于:
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
while ((first!=last)&&(first!=--last)) {
std::iter_swap (first,last);
++first;
}
}
需要头文件 #include<algorithm>
6. 进制转换
stoi 编译有风险,使用需谨慎 (hhhhh)
code.cpp:28:23: error: 'stoi' was not declared in this scope
code.cpp:133:25: error: 'stoi' was not declared in this scope
string toAns(long long n, int ne)//10进制转化为任意进制
{
if (n == 0)return "0";
string str;
while (n)
{
int tm = n % ne;
if (tm <= 9)
str += '0' + tm;
else
str += 'A' + tm - 10;
n /= ne;
}
reverse(str.begin(), str.end());
return str;
}
long long toTen(string str, int ne)//任意进制转化为10进制
{
long long ans = 0, arg = 1;
for (int i = str.size() - 1; i >= 0; --i)
{
if (str[i] <= '9')
ans += (str[i] - '0') * arg;
else
ans += (str[i] - 'A' + 10) * arg;
arg *= ne;
}
return ans;
}