static Vector<node> a[][][][] =new Vector[11][11][11][11]; for (int i = 1; i <= 9; i++) for (int j = 1; j <=9 ; j++) for (int k = 1; k <=9 ; k++) for (int l = 1; l <=9 ; l++) { node n=new node(i,j,k,l); a[k][l][(k+i)%10][j].add(n); a[k][l][(l+i)%10][j].add(n); a[k][l][(j+i)%10][j].add(n); a[k][l][i][(k+j)%10].add(n); a[k][l][i][(l+j)%10].add(n); a[k][l][i][(i+j)%10].add(n); }为什么会出现空指针表达
时间: 2024-04-09 19:34:08 浏览: 16
出现空指针异常的原因可能是因为在使用数组 `a` 之前没有对其进行初始化。在你的代码中,你定义了一个多维数组 `a`,但是没有为每个元素分配内存空间。因此,在尝试访问 `a[k][l][(k+i)%10][j]` 等元素时,会抛出空指针异常。
为了解决这个问题,你可以在初始化数组 `a` 时为每个元素分配内存空间。可以使用循环来为每个元素分配一个新的 `Vector` 对象,例如:
```java
static Vector<node> a[][][][] = new Vector[11][11][11][11];
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
for (int k = 1; k <= 9; k++) {
for (int l = 1; l <= 9; l++) {
a[i][j][k][l] = new Vector<node>();
}
}
}
}
```
这样初始化后,你就可以在后续代码中安全地访问数组 `a` 的元素,而不会出现空指针异常。
相关问题
C++中通过opencv读取txt文件,并将其解析为vector<string>格式
### 回答1:
在 C++ 中使用 OpenCV 读取 txt 文件并将其解析为 vector<string> 格式可以使用以下代码:
```
#include <opencv2/opencv.hpp>
#include <vector>
#include <string>
using namespace std;
int main() {
string filename = "example.txt";
vector<string> data;
ifstream file(filename);
if(file.is_open()) {
string line;
while(getline(file, line)) {
data.push_back(line);
}
file.close();
}
return 0;
}
```
在这段代码中,我们首先声明了一个文件名变量,一个 vector 变量 data 和一个 ifstream 变量 file。然后我们使用 ifstream 类的构造函数打开文件,并使用 is_open() 函数判断文件是否正常打开。如果文件打开成功,我们使用 getline() 函数逐行读取文件的内容并将每行的内容插入到 vector 变量 data 中。最后,我们关闭文件。
### 回答2:
在C中通过OpenCV读取txt文件,并将其解析为vector<string>格式的步骤如下:
1. 导入必要的头文件:
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <opencv2/opencv.hpp>
```
2. 创建一个函数来读取txt文件并解析为vector<string>格式:
```c++
std::vector<std::string> readTXTFile(const std::string& filename) {
std::vector<std::string> lines;
std::ifstream file(filename);
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
lines.push_back(line);
}
file.close();
} else {
std::cout << "Error opening file: " << filename << std::endl;
}
return lines;
}
```
3. 使用OpenCV中的 imread 函数读取txt文件:
```c++
std::string filename = "data.txt"; // txt文件名
cv::Mat image = cv::imread(filename, cv::IMREAD_GRAYSCALE);
```
4. 将读取的图像数据解析为vector<string>格式:
```c++
std::vector<std::string> lines;
for (int i = 0; i < image.rows; i++) {
std::string line;
for (int j = 0; j < image.cols; j++) {
int pixel_value = static_cast<int>(image.at<uchar>(i, j));
line += std::to_string(pixel_value) + " ";
}
lines.push_back(line);
}
```
5. 打印解析后的数据:
```c++
for (const auto& line : lines) {
std::cout << line << std::endl;
}
```
以上是通过OpenCV在C中读取txt文件并将其解析为vector<string>格式的步骤。
### 回答3:
在C++中利用OpenCV读取txt文件并解析为vector<string>格式可以通过以下步骤实现:
1. 首先,需要包含OpenCV的头文件和iostream头文件:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
```
2. 接下来,创建一个函数来读取txt文件并解析为vector<string>格式,函数原型如下:
```c++
std::vector<std::string> readTxtFile(const std::string& filename);
```
3. 在函数中,首先定义一个存储解析后字符串的vector<string>,例如:
```c++
std::vector<std::string> strVector;
```
4. 使用OpenCV的FileStorage类来读取txt文件:
```c++
cv::FileStorage fs(filename, cv::FileStorage::READ);
if (!fs.isOpened()) {
std::cerr << "Failed to open txt file." << std::endl;
return strVector;
}
```
5. 利用OpenCV的FileNode和FileNodeIterator对文件进行解析:
```c++
cv::FileNode rootNode = fs["data"];
if (rootNode.empty()) {
std::cerr << "Empty data node." << std::endl;
return strVector;
}
cv::FileNodeIterator it = rootNode.begin();
cv::FileNodeIterator it_end = rootNode.end();
for (; it != it_end; ++it) {
std::string str = (std::string)*it;
strVector.push_back(str);
}
```
6. 最后,返回解析后的字符串向量:
```c++
return strVector;
```
以下是完整的示例代码:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
std::vector<std::string> readTxtFile(const std::string& filename) {
std::vector<std::string> strVector;
cv::FileStorage fs(filename, cv::FileStorage::READ);
if (!fs.isOpened()) {
std::cerr << "Failed to open txt file." << std::endl;
return strVector;
}
cv::FileNode rootNode = fs["data"];
if (rootNode.empty()) {
std::cerr << "Empty data node." << std::endl;
return strVector;
}
cv::FileNodeIterator it = rootNode.begin();
cv::FileNodeIterator it_end = rootNode.end();
for (; it != it_end; ++it) {
std::string str = (std::string)*it;
strVector.push_back(str);
}
return strVector;
}
int main() {
std::vector<std::string> strVector = readTxtFile("data.txt");
for (const auto& str : strVector) {
std::cout << str << std::endl;
}
return 0;
}
```
这样,就可以通过OpenCV读取txt文件并解析为vector<string>格式。
代码每句话多啥意思// if ( num_iter < 10000 ) // cout << "current=" << current->index.transpose() << endl; if (current->index(0) == endPtr->index(0) && current->index(1) == endPtr->index(1) && current->index(2) == endPtr->index(2))判断当前节点是否为终点。 { // ros::Time time_2 = ros::Time::now(); // printf("\033[34mA star iter:%d, time:%.3f\033[0m\n",num_iter, (time_2 - time_1).toSec()*1000); // if((time_2 - time_1).toSec() > 0.1) // ROS_WARN("Time consume in A star path finding is %f", (time_2 - time_1).toSec() ); gridPath_ = retrievePath(current); return true; } current->state = GridNode::CLOSEDSET; //move current node from open set to closed set. for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) for (int dz = -1; dz <= 1; dz++) { if (dx == 0 && dy == 0 && dz == 0) continue; Vector3i neighborIdx; neighborIdx(0) = (current->index)(0) + dx; neighborIdx(1) = (current->index)(1) + dy; neighborIdx(2) = (current->index)(2) + dz; if (neighborIdx(0) < 1 || neighborIdx(0) >= POOL_SIZE_(0) - 1 || neighborIdx(1) < 1 || neighborIdx(1) >= POOL_SIZE_(1) - 1 || neighborIdx(2) < 1 || neighborIdx(2) >= POOL_SIZE_(2) - 1) { continue; } neighborPtr = GridNodeMap_[neighborIdx(0)][neighborIdx(1)][neighborIdx(2)]; neighborPtr->index = neighborIdx; bool flag_explored = neighborPtr->rounds == rounds_; if (flag_explored && neighborPtr->state == GridNode::CLOSEDSET) { continue; //in closed set. } neighborPtr->rounds = rounds_; if (checkOccupancy(Index2Coord(neighborPtr->index))) { continue; } double static_cost = sqrt(dx * dx + dy * dy + dz * dz); tentative_gScore = current->gScore + static_cost;
好的,我来解释一下每个语句的作用:
1. `if ( num_iter < 10000 ) cout << "current=" << current->index.transpose() << endl;`:这个语句用于在前10000次迭代中输出当前节点的索引,以便调试。
2. `if (current->index(0) == endPtr->index(0) && current->index(1) == endPtr->index(1) && current->index(2) == endPtr->index(2))`:这个语句用于判断当前节点是否为终点。
3. `gridPath_ = retrievePath(current);`:这个语句用于从起点到终点的路径,通过调用`retrievePath`函数来实现。
4. `current->state = GridNode::CLOSEDSET;`:这个语句用于将当前节点从开放集合移动到关闭集合。
5. `for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) for (int dz = -1; dz <= 1; dz++)`:这个语句用于遍历当前节点周围的所有相邻节点。
6. `if (dx == 0 && dy == 0 && dz == 0) continue;`:这个语句用于跳过当前节点本身。
7. `Vector3i neighborIdx; neighborIdx(0) = (current->index)(0) + dx; neighborIdx(1) = (current->index)(1) + dy; neighborIdx(2) = (current->index)(2) + dz;`:这个语句用于获取当前相邻节点的索引。
8. `if (neighborIdx(0) < 1 || neighborIdx(0) >= POOL_SIZE_(0) - 1 || neighborIdx(1) < 1 || neighborIdx(1) >= POOL_SIZE_(1) - 1 || neighborIdx(2) < 1 || neighborIdx(2) >= POOL_SIZE_(2) - 1) { continue; }`:这个语句用于判断当前相邻节点是否在地图范围内。
9. `neighborPtr = GridNodeMap_[neighborIdx(0)][neighborIdx(1)][neighborIdx(2)];`:这个语句用于获取当前相邻节点的指针。
10. `bool flag_explored = neighborPtr->rounds == rounds_;`:这个语句用于判断当前相邻节点是否已经被探索。
11. `if (flag_explored && neighborPtr->state == GridNode::CLOSEDSET) { continue; }`:这个语句用于跳过已经在关闭集合中的相邻节点。
12. `neighborPtr->rounds = rounds_;`:这个语句用于标记当前相邻节点已经被探索。
13. `if (checkOccupancy(Index2Coord(neighborPtr->index))) { continue; }`:这个语句用于跳过障碍物。
14. `double static_cost = sqrt(dx * dx + dy * dy + dz * dz);`:这个语句用于计算从当前节点到当前相邻节点的代价。
15. `tentative_gScore = current->gScore + static_cost;`:这个语句用于计算从起点到当前相邻节点的代价。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)