这个和编辑器没有关系,int的取值范围就是-2147483648~2147483647, 也就是10位
本题目电话号码是11位,不能用int来接受,要用string,各个元素再转数字-‘0’
C++ vector清空元素的三种方法
复制代码
#include <iostream>
#include <vector>
using namespace std;
//STL vector的几种清空容器(删除)办法
void test()
{
vector<int> vecnum;
vecnum.push_back(1);
vecnum.push_back(2);
vecnum.push_back(3);
vecnum.push_back(4);
vecnum.push_back(5);
vecnum.push_back(6);
vecnum.push_back(7);
//打印vector的大小和容量
cout << "old: size = " << vecnum.size() << " ; capacity = " << vecnum.capacity() << endl;
//1. clear(),清空元素,但不回收空间
//vecnum.clear();
/*
atoi()和stoi()的区别----数字字符串的处理
相同点:
①都是C++的字符处理函数,把数字字符串转换成int输出
②头文件都是#include<cstring>
不同点:
①atoi()的参数是 const char* ,因此对于一个字符串str我们必须调用 c_str()的方法把这个string转换成 const char*类型的,而stoi()的参数是const string*,不需要转化为 const char*;
如图:
②stoi()会做范围检查,默认范围是在int的范围内的,如果超出范围的话则会runtime error!
如图:
而atoi()不会做范围检查,如果超出范围的话,超出上界,则输出上界,超出下界,则输出下界;
代码:*/
//myfirst.cpp--displays a message
#include "stdafx.h"
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()
{
string s1 = "2147482", s2 = "-214748";
string s3 = "214748666666663", s4 = "-21474836488";
cout << stoi(s1) << endl;
cout << stoi(s2) << endl;
cout << atoi(s3.c_str()) << endl;
cout << atoi(s4.c_str()) << endl;
return 0;
}
#include <sstream>
using namespace std;
int strToInt(string s){
stringstream ss;
ss<<s;
int a;
ss>>a;
return a;
}
vector—sort–v.begin(),v.end()-set貌似不行—默认升序,降序的话自己写>
//18013820100
//1234567891011
//-----------------
//int[] arr = new int[]{8,3,2,1,0};
// 0,1,2,3,4
//int[] index = new int[] {3, 0, 4, 3, 1, 0, 2, 4, 3, 4, 4};
// 0, 1, 2, 3, 4, 5, 6, 7 ,8 ,9, 10
#include <cstdio>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
//#include <cstring>
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int main() {
set<int>s;
string tel;
cin >> tel;
// while (tel != 0) {
// int k = tel % 10;
// s.insert(k);
// tel /= 10;
// }
for(int i=0;i<tel.length();i++){
s.insert(tel[i]-'0');
}
int len = s.size();
set<int>::iterator it;
vector<int> v;
for (it = s.begin(); it != s.end(); it++) {
v.push_back(*it);//int[] arr = new int[]{8,3,2,1,0};
}
sort(v.begin(), v.end(), cmp);
int arr[11], i = 0;
for (int j = 0; j < len; j++) {
arr[i++] = v[j];//int[] arr = new int[]{8,3,2,1,0};
}
// while (tmp != 0) {
// int k = tmp % 10;
// for (int i = 0; i < len; i++) {
// if (k == arr[i]) {
// st.push(i);
// }
// }
// tmp /= 10;
// }
v.clear();
for(int i=0;i<tel.length();i++){
for(int j=0;j<len;j++){
if((tel[i]-'0')==arr[j])
v.push_back(j);
}
}
cout << "int[] arr = new int[]{";
for (int i = 0; i < len; i++) {
if (i != len - 1)cout << arr[i] << ",";
else cout << arr[i] << "};";
};
cout<<endl;
cout << "int[] index = new int[]{";
for(int i=0;i<v.size();i++){
if (i != 10)cout << v[i] << ",";
else cout << v[i] << "};";
};
return 0;
}