33,311
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
vector<string>& sortVecData(vector<string>& vecData) // 排序和删除重复 数字序列
{
sort(vecData.begin(), vecData.end()); //数据排序
vector<string>::iterator end_unique = unique(vecData.begin(), vecData.end()); // 移动重复到最后
vecData.erase(end_unique, vecData.end()); //删除重复
return vecData;
}
int main()
{
// 解释参考上面函数
char str[] = "jklmfghiqnopqaabbcdderstz";
sort(str, str + strlen(str));
cout << str << endl;
char* end_unique = unique(str, str + strlen(str));
cout << str << endl;
*end_unique = '\0';
cout << str << endl;
return 0;
}
#include <iostream>
#include <ctype.h>
using namespace std;
// string 找到非 isalnum 的字符索引,删除索引到最后
string& substr_isalnum(string& s)
{
for (size_t i = 0; i != s.size(); ++i) {
if (!isalnum(s[i])) { // 找到非 isalnum 的字符索引
s.erase(i, string::npos); // 删除索引到最后
break; // 跳出for 循环
}
}
return s;
}
int main()
{
string u = "abc1237*-=sdsd1237*aabc1237*a";
substr_isalnum(u);
cout << u << endl;
return 0;
}
#include <iostream>
#include <stdio.h>
using namespace std;
void test(bool bl)
{
if (bl)
throw "你好世界!";
else
throw "Hello world!"; // 抛出异常信息,C风格字符串
}
int main(int argc, char* argv[])
{
try { // 异常测试
// test(false); // 跳到catch (...)
while (1)
int* myarray = new int[1024 * 1024];
} catch (exception& e) {
cerr << "EXCEPCTION: " << e.what() << endl;
return -1;
} catch (...) {
cerr << "Untyped excepcion!!";
return -1;
}
/* try { // 异常测试
* test(false);
* } catch (const char* s) { // 只能只读使用异常信息
* cout << s << endl;
* return -88; // 直接结束程序
* }
*/
return 0;
}
/* try
* {
* cout << "Before throwing exception" << endl;
* throw 42; // 抛出异常号:数字
* cout << "Shouldn't ever see this" << endl;
* }
*
* catch (int error) // 获得异常号的副本,输出
* {
* cout << "Error: caught exception " << error << endl;
* }
*/