当前位置: 首页>>代码示例>>C++>>正文


C++ StringTokenizer::parse方法代码示例

本文整理汇总了C++中StringTokenizer::parse方法的典型用法代码示例。如果您正苦于以下问题:C++ StringTokenizer::parse方法的具体用法?C++ StringTokenizer::parse怎么用?C++ StringTokenizer::parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在StringTokenizer的用法示例。


在下文中一共展示了StringTokenizer::parse方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: loadGraph

void loadGraph(std::string edgeFile)
{
	// Open file for reading
	freopen(edgeFile.c_str(), "rt", stdin);

	edge_count = 0;
	vecEdges.clear();
	maxNode = -1;

	// Read line by line edge info
	while(gets(buff))
	{
		if(strlen(buff) == 0)
			break;
		edge_count++;

		StringTokenizer token;
		// tokenize using comma
		token.parse(buff, ",");
		std::vector<std::string> vecToken;
		vecToken.clear();
		token.getTokens(vecToken);

		// There should be exactly 9 values: edge_id, start_node_id, end_node_id, 
		// start_node_longitude, start_node_latitude, end_node_longitude, end_node_latitude, cost, reverse_cost
		if(vecToken.size() < 5)
			fprintf(stderr, "Error in %d edge\n", edge_count);

		// Populate Edge_t structure
		edge_astar_t tempEdge;
		tempEdge.id = atoi(vecToken[0].c_str());
		tempEdge.source = atoi(vecToken[1].c_str());
		tempEdge.target = atoi(vecToken[2].c_str());
		tempEdge.cost = atof(vecToken[3].c_str());
		tempEdge.reverse_cost = atof(vecToken[4].c_str());
		tempEdge.s_x = atof(vecToken[5].c_str());
		tempEdge.s_y = atof(vecToken[6].c_str());
		tempEdge.t_x = atof(vecToken[7].c_str());
		tempEdge.t_y = atof(vecToken[8].c_str());

		// Update max_node_id
		if(tempEdge.source > maxNode)
			maxNode = tempEdge.source;
		if(tempEdge.target > maxNode)
			maxNode = tempEdge.target;
		
		vecEdges.push_back(tempEdge);
	}

	edges = new edge_astar_t[edge_count];
	int i;

	for(i = 0; i < edge_count; i++)
	{
		edges[i] = vecEdges[i];
	}
	fclose(stdin);
}
开发者ID:DavidLiuGitHub,项目名称:pgrouting,代码行数:58,代码来源:BDATester.cpp

示例2: main

int main()
{
	int i;
	double cl;
	kase = 1;

	// The final output will be written in the outFile and the initial input will be read from inFile
	std::string outFile = "output.txt";
	std::string inFile = "input.txt";

	// Create the output file
	FILE *fpout = fopen(outFile.c_str(), "wt");
	fclose(fpout);

	// Open the input file
	FILE *fpin = fopen(inFile.c_str(), "rt");

	// Reading each of the cases, There may be two types of cases, with 4 parameters, with 5 parameters
	// There may also be comments that starts with #
	while(fgets(buff, 1000, fpin))
	{
		// No data
		if(strlen(buff) == 0)
			continue;
		// Comment
		if(buff[0] == '#')
			continue;
		StringTokenizer token;

		// tokeniize using space
		token.parse(buff, " \n\r");
		std::vector<std::string> vecToken; 
		token.getTokens(vecToken);

		int totParam = vecToken.size();

		// Not enough parameters
		if(totParam < 4)
			continue;

		// First token is the graph file name
		std::string graphFile = vecToken[0];

		// 2nd and 3rd tokens are start and end node id respectively
		int startNode = atoi(vecToken[1].c_str());
		int endNode = atoi(vecToken[2].c_str());

		// 4th Token is the result file for this query
		std::string pathFile = vecToken[3];
		int ind = pathFile.length() - 1;
		while(pathFile[ind] < 32)
		{
			pathFile[ind] = '\0';
			ind--;
		}

		// Load edge information from graph file
		loadGraph(graphFile);

		// Use bidirectional AStar to get the route
		BiDirAStar gdef;
		cl = clock();
		int res = gdef.bidir_astar(edges, edge_count, maxNode, startNode, endNode, &path, &path_count, &err_msg);
		cl = clock() - cl;

		// Write the route in the result file
		write_result(pathFile, res);
		
		// There is an answer file
		if(totParam > 4)
		{
			std::string ansFile = vecToken[4];
			ind = ansFile.length() - 1;
			while(ansFile[ind] < 32)
			{
				ansFile[ind] = '\0';
				ind--;
			}
			// Match and write result in the final output file
			match(pathFile, ansFile, outFile, cl / CLOCKS_PER_SEC);
		}
		else
		{
			// Provide information that the route is generated in path file.
			fpout = fopen(outFile.c_str(), "a+");
			fprintf(fpout, "Case %d: Path Written to file %s", kase, pathFile.c_str());
			fprintf(fpout, "Query Time: %lf sec\n\n", cl / CLOCKS_PER_SEC);
			fclose(fpout);
		}
		kase++;
		free(path);
		delete [] edges;
	}
	return 0;
}
开发者ID:DavidLiuGitHub,项目名称:pgrouting,代码行数:95,代码来源:BDATester.cpp


注:本文中的StringTokenizer::parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。