C++ 为什么向量数组打印的是内存地址而不是数组值?为什么在排序函数中会出现访问冲突?
我正在开发一个程序,它可以读取文本文件,并将单词存储在一个数组中,然后进行排序和搜索。我已经尝试过使用一个动态分配的数组来将单词放入文本文件中,但我得到的只是getline中的字符串无法读取错误(当我使用vectors时不会发生这种情况)。当我试图打印出向量数组以查看传递给排序函数的内容时,它会打印出数组的内存地址,而不是数组中存储的值 在我将向量数组传递给排序函数后,我在函数中的某些点上也会遇到读取访问冲突,我不明白为什么。我确实在代码中指出了问题的产生。请注意,我对编码非常陌生,这个程序还远未完成。我包含了我到目前为止所做的所有代码,因为如果我只是显示问题区域,我不认为我为什么会有这些错误是可以理解的。谢谢你的帮助C++ 为什么向量数组打印的是内存地址而不是数组值?为什么在排序函数中会出现访问冲突?,c++,arrays,function,vector,C++,Arrays,Function,Vector,我正在开发一个程序,它可以读取文本文件,并将单词存储在一个数组中,然后进行排序和搜索。我已经尝试过使用一个动态分配的数组来将单词放入文本文件中,但我得到的只是getline中的字符串无法读取错误(当我使用vectors时不会发生这种情况)。当我试图打印出向量数组以查看传递给排序函数的内容时,它会打印出数组的内存地址,而不是数组中存储的值 在我将向量数组传递给排序函数后,我在函数中的某些点上也会遇到读取访问冲突,我不明白为什么。我确实在代码中指出了问题的产生。请注意,我对编码非常陌生,这个程序还远
#include "flore0900header.h"
int main()
{
string name;
int num = 0, num2 = 0, count = 0;
vector<string> word; //to pass vector array to another function
cout << "Hello. Enter your name: ";
cin >> name;
cout << endl;
cout << "Welcome " << name << " This program lets you test 5 different sorting algorithms using texts files" << endl;
cout << endl;
cout << "Which text file would you like to search?" << endl;
cout << "=========================================" << endl;
cout << "1. The Blue Hotel" << endl;
cout << "2. 20,000 Leagues Under the Sea" << endl;
cout << "3. A Tale of Two Cities" << endl;
cin >> num;
count = text_select(num);
catch_array(&word); //passing vector array by reference. '&' is there because it won't work otherwise
cout << endl;
cout << "Which sorting algorithems would you like to use?" << endl;
cout << "========================================" << endl;
cout << "1. Selection" << endl;
cout << "2. Bubble" << endl;
cout << "3. Insertion" << endl;
cout << "4. Merge" << endl;
cout << "5. Quick" << endl;
cin >> num; cin >> num2;
sort_select(num, &word, count);//'&' is there because it won't work otherwise
sort_select(num2, &word, count);//'&' is there because it won't work otherwise
cout << endl;
cout << "Ok! Running algorithms..........." << endl;
}
#包括“flore0900header.h”
int main()
{
字符串名;
int num=0,num2=0,count=0;
向量字;//将向量数组传递给另一个函数
姓名;
cout没有“向量数组”这样的东西
可以有向量数组,但没有
然而,在这里,您的函数被编写为:
int partition(vector<string> a[], int from, int to)
// ^^
int分区(向量a[],int-from,int-to)
// ^^
我认为这样做是因为,在您在此处添加[]
和&
之前,您的函数似乎什么都不做
这是因为您是按值传递向量的,所以函数中的更改是对副本进行的,因此不会反映在调用范围中
您的更改允许代码进行编译,甚至可能在某些模糊的情况下“工作”,但这只是偶然的;向量和数组之间共享[INDEX]
语法。但您没有数组,因此假装您使用的函数是错误的。您的大多数访问都是越界的
还要注意的是,如果你没有bool操作符神圣的代码之母!请将你的代码压缩成一行行。阅读一行又一行不相关的代码是一件痛苦的事情。创建MRE的练习迫使你查看代码并尝试隔离问题,这通常可以帮助你解决问题,甚至不必问问题。由于您是新来的,请同时使用,并阅读和。欢迎使用堆栈溢出!“sort_select(num,&word,count);/“&”存在,因为它在其他情况下不起作用”通过猜测编程是行不通的。sort\u select
为什么要用向量数组呢?这个网站的代码太多了。你需要把你的问题缩小到某个特定的范围,然后以a的形式展示给我们。祝你好运!人们费心去看那些不相关的代码这一事实说明了一些真正的无聊。谢谢你非常感谢你指出这一点。我没有意识到我使用的语法是错误的,因为编译器直到我编译代码以检查它是否正常工作后才抱怨。这一更改修复了我遇到的问题。如果仅仅因为努力而不对此进行评价,那将是不礼貌的(当然这是正确的答案)。
#include "flore0900header.h"
//#include <vector>
int text_select(int num)
{
int count = 0;
vector<string> words(100000);
//vector<string>* word2 = new vector<string>[count];
std::ifstream infile;
if (num == 1)
{
infile.open("blue_hotel.txt");
if (infile.is_open())
{
cout << "file is open" << endl;
getline(infile, words[count]);//string read error when using not using vector
while (!infile.eof())
{
infile >> words[count];
count++;
infile.ignore();
getline(infile, words[count]);
}
}
else
{
cout << "file didn't open" << endl;
exit(1);
}
}
else if (num == 2)
{
infile.open("2under.txt");
if (infile.is_open())
{
cout << "file is open" << endl;
getline(infile, words[count]);
while (!infile.eof())
{
infile >> words[count];
count++;
infile.ignore();
getline(infile, words[count]);
}
}
else
{
cout << "file didn't open" << endl;
exit(1);
}
}
else if (num == 3)
{
infile.open("2city10.txt");
if (infile.is_open())
{
cout << "file is open" << endl;
getline(infile, words[count]);
while (!infile.eof())
{
infile >> words[count];
count++;
infile.ignore();
getline(infile, words[count]);
}
}
else
{
cout << "file didn't open" << endl;
exit(1);
}
}
else
cout << "not a valid choice try again" << endl;
infile.close();
catch_array(&words);//if I don't use '&' the vector won't pass through
return count;
}
vector<string> catch_array(vector<string> a[])
{
return *a;//if I don't put '*' before the 'a' I get an error
}
#include "flore0900header.h"
void sort_select(int num, vector<string> a[], int size)
{
int from = 0;
if (num == 1)
{
selection_sort(a, size);
}
else if (num == 2)
{
bubble_sort(a, size);
}
else if (num == 3)
{
insertion_sort(a, size);
}
else if (num == 4)
{
merge_sort(a, from, size);
}
else if (num == 5)
{
quick_sort(a, from, size);
}
else
cout << "not a valid pick try again" << endl;
}
#include "flore0900header.h"
void selection_sort(vector<string> a[], int size)
{
int next;
for (next = 0; next < size - 1; next++)
{
print(a, size);//to see what is being passed to the function
int min_pos = min_position(a, next, size - 1);
swap(a[next], a[min_pos]);
}
}
int min_position(vector<string> a[], int from, int to)
{
int min_pos = from;
for (int i = from + 1; i <= to; i++)
{
if (a[i] < a[min_pos])//read access violation happens here
{
min_pos = i;
}
}
return min_pos;
}
void print(vector<string> a[], int size)
{
for (int i = 0; i < size; i++)
{
cout << &a[i] << " ";//is printing memory locations instead of values
}
cout << endl;
}
#include "flore0900header.h"
void bubble_sort(vector<string> a[], int size)
{
for (int i = 0; i < size - 1; i++) //loop for recording no# of iteration needed to complete the sorting
{
int flagForSwap = 0; //creates a flag variable that accounts for wheather the swap function is called at all
//loop for counting comparisons
for (int j = 0; j < size - 1 - i; j++)
{
if (a[j] > a[j + 1]) //(read access violation happens here) compare adjacent array elements
{
swap(a[j], a[j + 1]); //completes the swap
flagForSwap = 1; // flag to 1 if swap is used
}
}
if (flagForSwap == 0) //breaks the iteration loop if inputed array is already sorted and no swap is needed
{
break;
}
}
}
void swap(int& x, int& y)
{
int temp = x; //creates a temp variable to store the value of the current element
x = y; // change the value of the current element to next element
y = temp; //assigns the value of temp to next element
}
#include "flore0900header.h"
void insertion_sort(vector<string> a[], int size)
{
for (int i = 1; i < size; i++)
{
vector<string> next = a[i];//read access violation happens here
int j = i;
while (j > 0 && a[j - 1] > next)
{
a[j] = a[j - 1];
j--;
}
a[j] = next;
}
}
#include "flore0900header.h"
void merge_sort(vector<string> a[], int from, int to)
{
if (from == to)
{
return;
}
int mid = (from + to) / 2;
merge_sort(a, from, mid);
merge_sort(a, mid + 1, to);
merge(a, from, mid, to);
}
void merge(vector<string> a[], int from, int mid, int to)
{
int n = to - from + 1;
vector<string>* b = new vector<string>[n];
int i1 = from;
int i2 = mid + 1;
int j = 0;
while (i1 <= mid && i2 <= to)
{
if (a[i1] < a[i2])//read access violation happens here
{
b[j] = a[i1];
i1++;
}
else
{
b[j] = a[i2];
i2++;
}
j++;
}
while (i1 <= mid)
{
b[j] = a[i1];
i1++;
j++;
}
while (i2 <= to)
{
b[j] = a[i2];
i2++;
j++;
}
for (j = 0; j < n; j++)
{
a[from + j] = b[j];
}
delete[] b;
}
#include "flore0900header.h"
void quick_sort(vector<string> a[], int from, int to)
{
if (from >= to)
{
return;
}
int p = partition(a, from, to);
quick_sort(a, from, p);
quick_sort(a, p + 1, to);
}
int partition(vector<string> a[], int from, int to)
{
vector<string> pivot = a[from];
int i = from - 1;
int j = to + 1;
while (i < j)
{
i++;
while (a[i] < pivot)//read access violation happens here
{
i++;
}
j--;
while (a[j] > pivot)
{
j--;
}
if (i < j)
{
swap(a[i], a[j]);
}
}
return j;
}
int partition(vector<string> a[], int from, int to)
// ^^