如何在 C++ 中静态转换 2D 向量大小()(size_t 到 int)?
How to static cast 2D vector size() (size_t to int) in C++?
我没有找到任何其他回答这个问题的帖子。这是一个c++程序。我在以下行收到错误:
int entryval = static_cast<int>(database[0].size());
错误提示:线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)
它使我注意到矢量文件中的 size() 文件,即:
{return static_cast<size_type>(this->__end_ - this->__begin_);}
我是否错误地格式化了类型转换?
//Species ID,Kingdom,Phylum,Class,Order,Family,Genus,Species,Authority,Infraspecific rank,Infraspecific name,Infraspecific authority,Stock/subpopulation,Synonyms,Common names (Eng),Common names (Fre),Common names (Spa),Red List status,Red List criteria,Red List criteria version,Year assessed,Population trend,Petitioned
//2055,ANIMALIA,CHORDATA,MAMMALIA,CARNIVORA,OTARIIDAE,Arctocephalus,australis,"(Zimmermann, 1783)",,,,,Arctophoca australis,South American Fur Seal,Otarie fourrure Australe,Oso Marino Austral,LC,,3.1,2016,increasing,N
//41664,ANIMALIA,CHORDATA,MAMMALIA,CARNIVORA,OTARIIDAE,Arctocephalus,forsteri,"(Lesson, 1828)",,,,,Arctocephalus australis subspecies forsteri|Arctophoca australis subspecies forsteri,"New Zealand Fur Seal, Antipodean Fur Seal, Australasian Fur Seal, Black Fur Seal, Long-nosed Fur Seal, South Australian Fur Seal",,,LC,,3.1,2015,increasing,N
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
std::string entry;
std::vector<std::vector<std::string> > database;
std::ifstream file("C:/Users/mewtwo/Desktop/Programs/Seals.csv");
if (file) {
while (std::getline(file, entry)) {
size_t dbsize = database.size();
database.resize(dbsize + 1);
std::istringstream ss(entry);
std::string field, push_field("");
bool no_quotes = true;
while (std::getline(ss, field, ',')) {
if (static_cast<size_t>(std::count(field.begin(), field.end(), '"')) % 2 != 0) {
no_quotes = !no_quotes;
field.erase(remove(field.begin(), field.end(), '\"'), field.end());
}
push_field += field + (no_quotes ? "" : ",");
if (no_quotes) {
database[dbsize].push_back(push_field);
push_field.resize(0);
}
}
}
}
int dbval = static_cast<int>(database.size());
int entryval = static_cast<int>(database[0].size());
int animalChoice;
int dataChoice;
int switchChoice;
bool moreData;
bool moreAnimal = true;
while (moreAnimal == true) {
std::cout << "*********************ENDANGERED ANIMALS*********************" << std::endl;
std::cout << "Choose an animal! (1-" << dbval - 1 << ")" << std::endl;
for (int x = 1; x < database.size(); x++) {
if (database[x][14] != ""){
std::cout << x << ". " << database[x][14] << std::endl;
}
else {
std::cout << x << ". " << database[x][6] << " " << database[x][7] << std::endl;
}
}
std::cout << "\nAnimal choice:";
while (!(std::cin >> animalChoice) || animalChoice <= 0 || animalChoice >= dbval ){
std::cout << "\nYou pressed an incorrect key. Try again1." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\nAnimal choice:";
}
moreData = true;
while (moreData == true) {
std::cout << "\nWhat data do you want to access about the ";
if(database[animalChoice][14] != ""){
std::cout << database[animalChoice][14] << std::endl;
}
else{
std::cout << database[animalChoice][6] << " " << database[animalChoice][7] << std::endl;
}
std::cout << "?" << std::endl;
for (int x = 0; x < entryval; x++) {
std::cout << x + 1 << "." << database[0][x] << std::endl;
}
std::cout << "\nData choice:";
while (!(std::cin >> dataChoice) || dataChoice <= 0 || dataChoice >= entryval ){
std::cout << "You pressed an incorrect key. Try again2." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\nData choice:";
}
std::cout << database[animalChoice][14] << std::endl;
std::cout << database[0][dataChoice - 1] << ": " << database[animalChoice][dataChoice - 1] << std::endl;
std::cout << "\nWould you like to access more data about the " << database[animalChoice][14];
if(database[animalChoice][14] != ""){
std::cout << database[animalChoice][14] << std::endl;
}
else{
std::cout << database[animalChoice][6] << " " << database[animalChoice][7] << std::endl;
}
std::cout << "?" << std::endl;
std::cout << "1. Yes" << std::endl;
std::cout << "2. Access data of a different animal" << std::endl;
std::cout << "3. Quit" << std::endl;
std::cout << "\nChoice:";
while (!(std::cin >> switchChoice) || switchChoice <= 0 || switchChoice >= 4 ){
std::cout << "You pressed an incorrect key. Try again3." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\nChoice:";
}
if (switchChoice == 1) {
moreData = true;
}
else if (switchChoice == 2) {
moreData = false;
break;
}
else if (switchChoice == 3) {
exit(1);
}
else {
std::cout << "\nPROGRAM FAILURE" << std::endl;
exit(1);
}
}
}
return 0;
}
此外,我的代码还有其他明显的问题吗?该代码采用 IUCN 的整个物种数据库,并使用菜单呈现数据。
address = 0x8
中的低值暗示代码试图通过空指针访问成员,该空指针来自无效的 "null" 引用 return 由 database[0]
.
operator[]
return 这种损坏的引用的唯一方法是访问越界,触发未定义的行为。在这种情况下,您的 std::vector
实现似乎只是盲目地通过其数据指针建立索引,当向量为空时该指针为 null。
底线:没有任何东西被插入到你的向量中。这可能是由于读取文件时出错造成的。您应该为您的代码添加错误检查并干净地报告它们。
我没有找到任何其他回答这个问题的帖子。这是一个c++程序。我在以下行收到错误:
int entryval = static_cast<int>(database[0].size());
错误提示:线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8) 它使我注意到矢量文件中的 size() 文件,即:
{return static_cast<size_type>(this->__end_ - this->__begin_);}
我是否错误地格式化了类型转换?
//Species ID,Kingdom,Phylum,Class,Order,Family,Genus,Species,Authority,Infraspecific rank,Infraspecific name,Infraspecific authority,Stock/subpopulation,Synonyms,Common names (Eng),Common names (Fre),Common names (Spa),Red List status,Red List criteria,Red List criteria version,Year assessed,Population trend,Petitioned
//2055,ANIMALIA,CHORDATA,MAMMALIA,CARNIVORA,OTARIIDAE,Arctocephalus,australis,"(Zimmermann, 1783)",,,,,Arctophoca australis,South American Fur Seal,Otarie fourrure Australe,Oso Marino Austral,LC,,3.1,2016,increasing,N
//41664,ANIMALIA,CHORDATA,MAMMALIA,CARNIVORA,OTARIIDAE,Arctocephalus,forsteri,"(Lesson, 1828)",,,,,Arctocephalus australis subspecies forsteri|Arctophoca australis subspecies forsteri,"New Zealand Fur Seal, Antipodean Fur Seal, Australasian Fur Seal, Black Fur Seal, Long-nosed Fur Seal, South Australian Fur Seal",,,LC,,3.1,2015,increasing,N
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
std::string entry;
std::vector<std::vector<std::string> > database;
std::ifstream file("C:/Users/mewtwo/Desktop/Programs/Seals.csv");
if (file) {
while (std::getline(file, entry)) {
size_t dbsize = database.size();
database.resize(dbsize + 1);
std::istringstream ss(entry);
std::string field, push_field("");
bool no_quotes = true;
while (std::getline(ss, field, ',')) {
if (static_cast<size_t>(std::count(field.begin(), field.end(), '"')) % 2 != 0) {
no_quotes = !no_quotes;
field.erase(remove(field.begin(), field.end(), '\"'), field.end());
}
push_field += field + (no_quotes ? "" : ",");
if (no_quotes) {
database[dbsize].push_back(push_field);
push_field.resize(0);
}
}
}
}
int dbval = static_cast<int>(database.size());
int entryval = static_cast<int>(database[0].size());
int animalChoice;
int dataChoice;
int switchChoice;
bool moreData;
bool moreAnimal = true;
while (moreAnimal == true) {
std::cout << "*********************ENDANGERED ANIMALS*********************" << std::endl;
std::cout << "Choose an animal! (1-" << dbval - 1 << ")" << std::endl;
for (int x = 1; x < database.size(); x++) {
if (database[x][14] != ""){
std::cout << x << ". " << database[x][14] << std::endl;
}
else {
std::cout << x << ". " << database[x][6] << " " << database[x][7] << std::endl;
}
}
std::cout << "\nAnimal choice:";
while (!(std::cin >> animalChoice) || animalChoice <= 0 || animalChoice >= dbval ){
std::cout << "\nYou pressed an incorrect key. Try again1." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\nAnimal choice:";
}
moreData = true;
while (moreData == true) {
std::cout << "\nWhat data do you want to access about the ";
if(database[animalChoice][14] != ""){
std::cout << database[animalChoice][14] << std::endl;
}
else{
std::cout << database[animalChoice][6] << " " << database[animalChoice][7] << std::endl;
}
std::cout << "?" << std::endl;
for (int x = 0; x < entryval; x++) {
std::cout << x + 1 << "." << database[0][x] << std::endl;
}
std::cout << "\nData choice:";
while (!(std::cin >> dataChoice) || dataChoice <= 0 || dataChoice >= entryval ){
std::cout << "You pressed an incorrect key. Try again2." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\nData choice:";
}
std::cout << database[animalChoice][14] << std::endl;
std::cout << database[0][dataChoice - 1] << ": " << database[animalChoice][dataChoice - 1] << std::endl;
std::cout << "\nWould you like to access more data about the " << database[animalChoice][14];
if(database[animalChoice][14] != ""){
std::cout << database[animalChoice][14] << std::endl;
}
else{
std::cout << database[animalChoice][6] << " " << database[animalChoice][7] << std::endl;
}
std::cout << "?" << std::endl;
std::cout << "1. Yes" << std::endl;
std::cout << "2. Access data of a different animal" << std::endl;
std::cout << "3. Quit" << std::endl;
std::cout << "\nChoice:";
while (!(std::cin >> switchChoice) || switchChoice <= 0 || switchChoice >= 4 ){
std::cout << "You pressed an incorrect key. Try again3." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\nChoice:";
}
if (switchChoice == 1) {
moreData = true;
}
else if (switchChoice == 2) {
moreData = false;
break;
}
else if (switchChoice == 3) {
exit(1);
}
else {
std::cout << "\nPROGRAM FAILURE" << std::endl;
exit(1);
}
}
}
return 0;
}
此外,我的代码还有其他明显的问题吗?该代码采用 IUCN 的整个物种数据库,并使用菜单呈现数据。
address = 0x8
中的低值暗示代码试图通过空指针访问成员,该空指针来自无效的 "null" 引用 return 由 database[0]
.
operator[]
return 这种损坏的引用的唯一方法是访问越界,触发未定义的行为。在这种情况下,您的 std::vector
实现似乎只是盲目地通过其数据指针建立索引,当向量为空时该指针为 null。
底线:没有任何东西被插入到你的向量中。这可能是由于读取文件时出错造成的。您应该为您的代码添加错误检查并干净地报告它们。