进制转换
题目链接 : https://www.luogu.com.cn/problem/P1143我的代码 :
#include <bits/stdc++.h>
using namespace std;
int base, outbase;
string num;
int main(){
ios::sync_with_stdio(0);
cin >> base >> num >> outbase;
to_chars(num.begin(), num.end(), strtol(num, NULL, base), outbase);
for(auto it = num.begin(); it != num.end(); it++){
toupper(*it);
}
cout << num << endl;
return 0;
}
出错了 , 本来想学学看看进制转换的函数的 , 然后就不太会用 to_chars() 和 strtol() , 然后不理解 to_chars()第三个参数 " 要转的值 " 是啥意思
看文章看不太懂 , 求帮助 求救 , 这玩意为啥出错, 然后这两个函数怎么用 本帖最后由 jhq999 于 2022-6-17 21:50 编辑
柿子饼同学 发表于 2022-6-17 20:46
求救 , 这玩意为啥出错, 然后这两个函数怎么用
#include <bits/stdc++.h>
#include <charconv>
using namespace std;
int base, outbase;
string num;
int main(){
ios::sync_with_stdio(0);
cin >> base >> num >> outbase;
int a=strtol(num.c_str(), NULL, base);
num.resize(32);
to_chars((char*)(num.c_str()), ((char*)(num.c_str()))+32, a, outbase);
/*for(auto it = num.begin(); it != num.end(); it++){
toupper(*it);
}*/
cout << num << endl;
return 0;
}
#include <iostream>
#include <charconv>
#include <string>
#include <array>
using std::to_chars, std::errc;
using std::string, std::array;
using std::cout, std::endl;
int main() {
string str = "1234";
array<char, 1024> buff;
int num = strtol(str.c_str(), nullptr, 10);
const auto & = to_chars(buff.data(), buff.data() + buff.size(), num, 16);
if(ec == std::errc()) cout << string(buff.data(), ptr) << endl;
return 0;
}
题目应该是要求自行写函数转换吧?#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
using std::string, std::map, std::reverse, std::vector;
string convert(string prev, int from, int to) {
reverse(prev.begin(), prev.end());
string dict = "0123456789ABCDEF", result = "";
int n = 0, res = 0;
map<char, int> nums;
for (int i = 0; i < 16; ++i) nums] = i;
for (const char& c : prev) {
res += pow(from, n) * nums;
n++;
}
vector<char> ans(dict.begin(), dict.end());
while (res) {
n = res % to;
result += ans;
res /= to;
}
reverse(result.begin(), result.end());
return result;
}
using std::cout, std::endl;
int main(void) {
string nums = "4D2";
cout << convert(nums, 16, 2);
}10011010010 傻眼貓咪 发表于 2022-6-17 22:32
题目应该是要求自行写函数转换吧?
额,其实吧,我只是想学下题解里的函数。谢谢{:10_245:} 柿子饼同学 发表于 2022-6-17 22:44
额,其实吧,我只是想学下题解里的函数。谢谢
{:10_257:}
傻眼貓咪 发表于 2022-6-17 22:55
{:10_254:} jhq999 发表于 2022-6-17 21:49
谢谢{:10_254:}
我有一点疑问:
strol() 中间的那个参数为啥都填 NULL
strtol 就是把指定的字符串通过 base 进制转换成一个 10 进制的 long ?
to_chars 是只能接受c的字符数组?
所以 to_chars 接受一个空字符串 , 然后传入一个数, 把它变成 base 进制的字符串?
{:10_254:} 人造人 发表于 2022-6-17 22:26
谢谢回答{:10_254:}
有点疑问:
第 14 行这个啥意思看不懂{:10_245:}
std::errc 是什么? 柿子饼同学 发表于 2022-6-17 23:11
谢谢回答
有点疑问:
第 14 行这个啥意思看不懂
https://en.cppreference.com/w/cpp/utility/to_chars 本帖最后由 jhq999 于 2022-6-18 06:20 编辑
柿子饼同学 发表于 2022-6-17 23:06
谢谢
我有一点疑问:
strol() 中间的那个参数为啥都填 NULL
360百科
函数定义
long int strtol(const char *nptr,char **endptr,int base);
折叠编辑本段函数说明
参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制等。当base值为0时则是采用10进制做转换,但遇到如'0x'前置字符则会使用16进制做转换、遇到'0'前置字符而不是'0x'的时候会使用8进制做转换。
一开始strtol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时('\0')结束转换,并将转换数值返回。参数endptr指向停止转换的位置,若字符串nptr的所有字符都成功转换成数字则endptr指向串结束符'\0'。判断是否转换成功,应检查**endptr是否为'\0'。
char* stop;
inta=strtol(num.c_str(), &stop, base);
if('\0'==**stop){}
人造人 发表于 2022-6-17 23:33
https://en.cppreference.com/w/cpp/utility/to_chars
额 , 写 to_chars 的时候它是有这个提示的 , 但是编译之后说未定义是什么情况#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
string num;
int base, outbase;
int main(){
ios::sync_with_stdio(0);
cin >> base >> num >> outbase;
ull n = stoull(num, nullptr, base);
num.resize(40);
to_chars(num.data(), num.data() + num.size(), n, outbase);
cout << num << endl;
return 0;
}
柿子饼同学 发表于 2022-6-18 15:08
额 , 写 to_chars 的时候它是有这个提示的 , 但是编译之后说未定义是什么情况
看看你的多少
$ gcc --version
gcc (GCC) 12.1.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ 人造人 发表于 2022-6-18 17:00
看看你的多少
额, vscode 怎么看{:10_266:} 柿子饼同学 发表于 2022-6-18 19:03
额, vscode 怎么看
我不用vscode,所以不知道
人造人 发表于 2022-6-18 17:00
看看你的多少
额这个怎么搞
10.3.0 柿子饼同学 发表于 2022-6-18 20:23
额这个怎么搞
10.3.0
https://gcc.gnu.org/onlinedocs/gcc-10.3.0/gcc/Standards.html
The default, if no C++ language dialect options are given, is -std=gnu++14.
https://en.cppreference.com/w/cpp/utility/to_chars
std::to_chars_result to_chars( char* first, char* last,
/*see below*/ value, int base = 10 );
(1) (since C++17)
你的编译器默认使用的-std=gnu++14
to_chars最低要求C++17
所以,编译命令中你必须指定std
g++ -g -Wall -std=gnu++17 -o main main.cpp 人造人 发表于 2022-6-19 01:11
https://gcc.gnu.org/onlinedocs/gcc-10.3.0/gcc/Standards.html
PS C:\Users\86177\Desktop\vscodec++> cd "c:\Users\86177\Desktop\vscodec++\" ; if ($?) { g++ -fexec-charset=GBK 刷题测试文件.cpp -o 刷题测试文件 } ; if ($?) { .\刷题测试文件 }
刷题测试文件.cpp: In function 'int main()':
刷题测试文件.cpp:12:17: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
12 | const auto & = to_chars(buff.data(), buff.data() + buff.size(), num, 16);
| ^
4d2
它现在说结构化绑定要c++17 , 但是我已经写上去了 , 怎么办{:10_266:} 柿子饼同学 发表于 2022-6-19 13:19
它现在说结构化绑定要c++17 , 但是我已经写上去了 , 怎么办
这明显就是编译命令不对么
g++ -fexec-charset=GBK 刷题测试文件.cpp -o 刷题测试文件
页:
[1]
2