c++ 我怎样才能把结果写入一个新的json文件?

zaqlnxep  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(144)

我如何将结果写入新的json文件?我已将代码写入json文件,并希望将结果写入新的json文件。在main方法中,我收到内存访问错误。结果应显示在nearestNeighbourHeuristics函数的新Json日期i中

void nearestNeighbourHeuristics(int startingNode) {
    int startingIndex = startingNode - 1;
    bool visited[m_NoOfNodes];
    for (int i=0; i < m_NoOfNodes; i++) {
        visited[i] = false;
    }
    int noOfVisitedNodes = 1;
    std::vector<int> finalPath;
    // start from the starting index and loop until you visit all nodes
    int curr_node = startingIndex;
    visited[curr_node] = true;
    finalPath.push_back(curr_node);
    while(noOfVisitedNodes < m_NoOfNodes) {
        curr_node = getNearestNeighbour(curr_node, visited);
        if(curr_node != -1) {
            visited[curr_node] = true;
            finalPath.push_back(curr_node);
        }
        noOfVisitedNodes += 1;
    }
    std::cout << "Path with nearest neighbour approach" << std::endl;
    finalPath.push_back(startingIndex);
    // Print final path
    for(int i=0; i<finalPath.size(); i++) {
        std::cout << finalPath[i]+1 << " ";
    }
    std::cout << std::endl;
    // Final path length
    double path_length = getPathLength(finalPath);
    std::cout << "Path length with nearest neighbour: " << path_length << std::endl;
}

int main(int arg, char *argv[]){
vector<array<double,2>> nodesVector;
std::ifstream file1;
file1.open("bb.json");

Tsppd t(file1);
t.nearestNeighbourHeuristics(1);
return 0;

相关问题