C++ 无法将字符数组变量分配给字符串变量

C++ Unable to assign Character Array Variable to String Variable

本文关键字:变量 分配 字符串 数组 字符 C++      更新时间:2023-10-16

我需要帮助。

我正在尝试创建一个矢量结构,其中 char 数组内的字符串内容会自动分配给同一结构中的另一个 STRING 变量。

无数个小时后,我都没能想通。当我使用诸如字符串((之类的函数时。它不会复制任何东西。我似乎无法将字符变量分配给字符串变量。请指教。

#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int NAME_SIZE = 25;
struct tableInfo
{
char name2[NAME_SIZE];
string name = str(name2); // I need name string to equal name2 character variable
string info;
string link;
};
vector<tableInfo> table(6);
bool compareByWord(const tableInfo &lhs, const tableInfo &rhs);

int main()
{
cin.getline(table[0].name2, 51);
cin.getline(table[1].name2, 51);
cin.getline(table[2].name2, 51);
cin.getline(table[3].name2, 51);
cin.getline(table[4].name2, 51);

sort(table.begin(), table.end(), compareByWord);
cout << table[0].name << endl;
cout << table[1].name << endl;
cout << table[2].name << endl;
cout << table[3].name << endl;
cout << table[4].name << endl;
}

bool compareByWord(const tableInfo &lhs, const tableInfo &rhs)
{
unsigned int length = lhs.name.length();
if (rhs.name.length() < length)
length = rhs.name.length();
int sameLetters = 0;
for (unsigned int i = 0; i < length; ++i)
{
if (sameLetters == length)
return false;
if (tolower(lhs.name[i]) == tolower(rhs.name[i]))
{
++sameLetters;
continue;
}
return(lhs.name[i] < rhs.name[i]);
return(lhs.name[i] < rhs.name[i]);
}
return false;
}

如果您希望在结构(或类(中发生某些事情,请考虑使用构造函数确保它发生。您将需要更改矢量,以便您不能再默认构造结构,但这没关系。

通过强制两个字段包含相同的数据(尽管 25 个字段不够长(,这是有效的:

const int NAME_SIZE = 25;
struct tableInfo
{
tableInfo(char * whatever); //Make it so
char name2[NAME_SIZE];
string name;
string info;
string link;
};

tableInfo::tableInfo(char *whatever)
: name(whatever)
{
strncpy(name2, whatever, NAME_SIZE);  //Now they match - unless whatever was too big
}
//vector<tableInfo> table(6);//no longer ok with the non-default constructor
bool compareByWord(const tableInfo &lhs, const tableInfo &rhs);
int main()
{
vector<tableInfo> table;
for (int i = 0; i < 5; ++i)
{
char name2[NAME_SIZE];
cin.getline(name2, 51); //Do you really mean 51?
// It's smaller than NAME_SIZE
table.emplace_back(name2);
}
sort(table.begin(), table.end(), compareByWord);
cout << table[0].name << endl;
cout << table[1].name << endl;
cout << table[2].name << endl;
cout << table[3].name << endl;
cout << table[4].name << endl;
}

这样做怎么样:

struct tableInfo
{
char name2[NAME_SIZE];
string name;
string info;
string link;
tableInfo(const char* in_name)
{
memset(name2, 0, NAME_SIZE);
strncpy(name2, in_name, NAME_SIZE - 1);
name = string(name2);
}
};

如何使用:

char tmp[NAME_SIZE];
cin.getline(tmp, 51);
table[0] = tableInfo(tmp);

我认为你甚至不需要结构中的数组。只需使用std::string.getline支持读入std::string,您可以使用str[]访问单个字符。如果需要获取指向数据的指针,可以使用str.c_str()

如果你真的需要在结构中使用数组,你可以创建一个成员函数来返回一个std::string

struct tableName {
char name[NAME_SIZE];
std::string info;
std::string link;
std::string nameString() const { return name; }
};