本帖最后由 bin554385863 于 2019-8-17 21:23 编辑
2019年8月17日12:10:30#include <iostream>
#include <vector>
#include <cctype>
#include <cmath>
/*判断字符串是不是一个整数*/
bool isnumber(std::string &strargs)
{
bool flag = true;
for (char cargs : strargs)
{
if (!isdigit(cargs))
{
flag = false;
break;
}
}
return flag;
}
/*将数字字符串转换成整数*/
std::vector<int> tonumber(std::string &strargs)
{
std::vector<int> vecargs;
const int size = strargs.size();
double result = 0;
for (size_t i = 0; i < size; i++)
{
result += (strargs[i] - 48) * pow(10, size - 1 - i);
}
vecargs.push_back(result);
result = 0;
return vecargs;
}
*****************************************************************************#include <iostream>
#include <vector>
#include <cctype>
#include <cmath>
#include "E:\Users\86184\Documents\Code\Study\0_0_0_MyC++func.cpp"
/*使用迭代器使输入的数字翻倍输出*/
int main(int argc, char const * argv[])
{
using namespace std;
double ynum = 0;
string wnum;
vector<int> intnum;
while (true)
{
cout << "请输入数字" << endl;
cin >> wnum;
if (!isnumber(wnum))
{
cout << "输入含有其他字符,请重新输入" << endl;
continue;
}
intnum = tonumber(wnum);
vector<int>::iterator it = intnum.begin();
for (; it != intnum.end(); ++it)
{
*it = *it * 2;
}
for (int num : intnum)
{
cout << wnum << " X 2 = " << num << " ";
}
cout<<endl;
}
return 0;
}
===================================================================
E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-0mcjprfn.ln5 --stdout=Microsoft-MIEngine-Out-aiq00ynt.j4f --stderr=Microsoft-MIEngine-Error-gsbuvl41.e2z --pid=Microsoft-MIEngine-Pid-e3rxfvlh.qsi "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
请输入数字
q
输入含有其他字符,请重新输入
请输入数字
+
输入含有其他字符,请重新输入
请输入数字
123 1000 3
123 X 2 = 246
请输入数字
1000 X 2 = 2000
请输入数字
3 X 2 = 6
请输入数字
E:\Users\86184\Documents\Code>
==================================================================
数组和向量#include <iostream>
#include <string>
#include <vector>
/*使用数组初始化集合*/
int main(int argc, char const *argv[])
{
using namespace std;
int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 02, 35, 3, 54};
/*使用数字初始化集合*/
vector<int> vec(begin(arr), end(arr));//使用begin()和end()函数,数组名作为参数,获取数组的头指针和尾部指针
vector<int> vecx(begin(arr) + 5, begin(arr) + 10);
for (int i : vec)
{
cout << i << " ";
}
cout << endl;
for (int j : vecx)
{
cout << j << " ";
}
return 0;
}
------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.16299.1087]
(c) 2017 Microsoft Corporation。保留所有权利。
E:\Users\86184\Documents\Code>c:\Users\86184\.vscode\extensions\ms-vscode.cpptools-0.24.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-kpilswng.fil --stdout=Microsoft-MIEngine-Out-lllfevgw.4pb --stderr=Microsoft-MIEngine-Error-f4342yjf.phl --pid=Microsoft-MIEngine-Pid-edkkjwrg.zlu "--dbgExe=E:\My Program\MinGW\bin\gdb.exe" --interpreter=mi
0 1 2 3 4 5 6 7 8 9 1 2 35 3 54
5 6 7 8 9
E:\Users\86184\Documents\Code>
|