gpt4 book ai didi

c++ - 使用 std、vector 和 bitvec 的 C++ 代码中的错误以及如何从用户那里获取输入

转载 作者:行者123 更新时间:2023-11-28 07:18:40 24 4
gpt4 key购买 nike

我在这里发布了一个问题堆栈溢出并得到了这段代码,我还有一些关于代码的问题,所以 iv 提出了一个单独的问题,因为问题是不同的。

下面的代码给我的错误如下(由注释表示):

#include <iostream>
#include <string>
#include <bitset>
using namespace std;
#include <vector>

int main() {

string s;
int i;
int j;


//how to take string s as input from user and feed it to the code?
std::vector<uint16_t> bitvec;
unsigned char* cp = s.c_str()+1;
while (*cp) {
uint16_t bits = *(cp-1)>>8 + *(cp); // error1
bitvec.push_back(bits);
}


uint32_t sum=0;

for(std::vector<int16_t>::iterator j=bitvec.begin();j!=bitvec.end();++j) { //error2
sum += *j;
uint16_t overflow = sum>>16; //capture the overflow bit, move it back to lsb
sum &= (1<<16)-1; //clear the overflow
sum += overflow; //add it back as lsb
}

//how can i see output of final sum+

//错误1:std::vector bitvec;//对于这一行,我收到“uint16_t 未在范围内声明”和“模板参数无效”的错误

//error2: j之前的声明中的有效类型(什么意思)

问题 1:如何从用户那里获取字符串 s 的输入?为什么我不能使用 cout << "enter string"然后使用 getline(cin,s)?同样我怎样才能看到输出?我可以使用 cout<

问题 2:我是否遗漏了代码可能需要的任何 header ?

最佳答案

uint16_t 通常定义在 #include <stdint.h> 中在 Linux 上。您需要包含此头文件。

关于c++ - 使用 std、vector 和 bitvec 的 C++ 代码中的错误以及如何从用户那里获取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19867208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com