C++ 编译器认为int是字符串。发生了什么事?
我尝试使用这个选择排序算法对数组的内容进行排序。但是,我使用的代码块编译器给了我一个错误,说明无法在赋值|中将'std::string{aka std::basic_string}'转换为'int'。这是指读取minvalue=wordarray[startscan]的行;minvalue和startscan都是整数,wordarray是数组。这是我的密码:C++ 编译器认为int是字符串。发生了什么事?,c++,arrays,sorting,C++,Arrays,Sorting,我尝试使用这个选择排序算法对数组的内容进行排序。但是,我使用的代码块编译器给了我一个错误,说明无法在赋值|中将'std::string{aka std::basic_string}'转换为'int'。这是指读取minvalue=wordarray[startscan]的行;minvalue和startscan都是整数,wordarray是数组。这是我的密码: #include <iostream> #include <fstream> using namespa
#include <iostream>
#include <fstream>
using namespace std;
string wordarray [1024];
int main()
{
int wordcount = 0;
string filename;
cout << "Please enter the name and location of your file." << endl;
cin >> filename;
ifstream testfile;
testfile.open (filename.c_str());
for (int i=0; i < 1024; ++i)
{
testfile >> wordarray[i];
cout << wordarray[i] << endl;
}
testfile.close();
}
void arraysort (int size)
{
int startscan, minindex;
int minvalue;
for (startscan = 0; startscan < (size - 1); startscan++)
{
minindex = startscan;
minvalue = wordarray[startscan]; //here
for (int index = startscan + 1; index < size; index ++)
{
if (wordarray[index] < minvalue)
{
minvalue = wordarray[index];
minindex = index;
}
}
wordarray[minindex] = wordarray[startscan];
wordarray[startscan] = minvalue;
}
}
字符串字数组[1024];是字符串的数组。从字符串数组中获取元素会得到一个字符串:
#include <iostream>
#include <fstream>
using namespace std;
string wordarray [1024];
int main()
{
int wordcount = 0;
string filename;
cout << "Please enter the name and location of your file." << endl;
cin >> filename;
ifstream testfile;
testfile.open (filename.c_str());
for (int i=0; i < 1024; ++i)
{
testfile >> wordarray[i];
cout << wordarray[i] << endl;
}
testfile.close();
}
void arraysort (int size)
{
int startscan, minindex;
int minvalue;
for (startscan = 0; startscan < (size - 1); startscan++)
{
minindex = startscan;
minvalue = wordarray[startscan]; //here
for (int index = startscan + 1; index < size; index ++)
{
if (wordarray[index] < minvalue)
{
minvalue = wordarray[index];
minindex = index;
}
}
wordarray[minindex] = wordarray[startscan];
wordarray[startscan] = minvalue;
}
}
auto word = wordarray[someInt]; //word is a string
<>在C++中,没有从STD::string到int的转换:< P>错误消息以清晰的方式描述您的代码。
#include <iostream>
#include <fstream>
using namespace std;
string wordarray [1024];
int main()
{
int wordcount = 0;
string filename;
cout << "Please enter the name and location of your file." << endl;
cin >> filename;
ifstream testfile;
testfile.open (filename.c_str());
for (int i=0; i < 1024; ++i)
{
testfile >> wordarray[i];
cout << wordarray[i] << endl;
}
testfile.close();
}
void arraysort (int size)
{
int startscan, minindex;
int minvalue;
for (startscan = 0; startscan < (size - 1); startscan++)
{
minindex = startscan;
minvalue = wordarray[startscan]; //here
for (int index = startscan + 1; index < size; index ++)
{
if (wordarray[index] < minvalue)
{
minvalue = wordarray[index];
minindex = index;
}
}
wordarray[minindex] = wordarray[startscan];
wordarray[startscan] = minvalue;
}
}
string wordarray [1024]; // strings
/**/
int minvalue; // int
/**/
minvalue = wordarray[startscan]; // attempt to assign a string to an int
您必须重新考虑该行应该做什么。Code::Blocks不是编译器。错误信息是非常文字化的。编译器不会像你想象的那样思考。那么为什么它认为一个声明为int的值是字符串呢?我说了,编译器不会像你想象的那样思考。>>那么为什么它认为一个声明为int的值是字符串呢?事实并非如此。它只是说你不能给一个字符串赋值,我想答案包括我,你对情况和涉及的数据类型有非常不同的看法。这不一定是你的错,可能会阻止你找到任何有用的答案。如果你在网上看到我,我会主动提供一些互动式的聊天帮助,最好是在几个小时的聊天时间内,或者在这里用@Yunnosch写评论来引起我的注意,比如说6小时。
#include <iostream>
#include <fstream>
using namespace std;
string wordarray [1024];
int main()
{
int wordcount = 0;
string filename;
cout << "Please enter the name and location of your file." << endl;
cin >> filename;
ifstream testfile;
testfile.open (filename.c_str());
for (int i=0; i < 1024; ++i)
{
testfile >> wordarray[i];
cout << wordarray[i] << endl;
}
testfile.close();
}
void arraysort (int size)
{
int startscan, minindex;
int minvalue;
for (startscan = 0; startscan < (size - 1); startscan++)
{
minindex = startscan;
minvalue = wordarray[startscan]; //here
for (int index = startscan + 1; index < size; index ++)
{
if (wordarray[index] < minvalue)
{
minvalue = wordarray[index];
minindex = index;
}
}
wordarray[minindex] = wordarray[startscan];
wordarray[startscan] = minvalue;
}
}