简体   繁体   English

问题从不同大小的向量输出数据

[英]Issue outputting data from vectors of different sizes

for(int i=1; i<distances.size(); i++)
cout << "Distance between " << names[i-1] << " and " << names[i] << " is: " << distances[i] << ", " << endl;

This is a snippet of my program, a quick bit of info: The program takes input from the user to find out where they would like to visit and then prints(issue lies here) the distances between destinations.这是我的程序的一个片段,一个简短的信息:程序从用户那里获取输入来找出他们想要访问的地方,然后打印(问题出在这里)目的地之间的距离。 So all the calculations work, but the issue is with my cout line, because one distance is created for the first two inputs, and then one more distance for every other input.所以所有计算都有效,但问题在于我的 cout 线,因为为前两个输入创建了一个距离,然后为每个其他输入创建了一个距离。 So in short, distances.size is 1 less than names.size.所以简而言之,distances.size 比 names.size 小 1。 What this means is that no names are put out for the first distance recorded since I'm using 'i' to loop the output.这意味着由于我使用“i”来循环输出,因此没有为记录的第一个距离输出任何名称。 If I enter 3 destinations, there is one output.如果我输入 3 个目的地,则有一个输出。 4 destinations, 2 outputs, but 2 destinations, there are no outputs. 4个目的地,2个输出,但是2个目的地,没有输出。 What can I do?我能做什么? I hope I've made my issue clear.我希望我已经把我的问题说清楚了。

Maybe you should think about your software Design...也许您应该考虑一下您的软件设计...

I assume, that if you have 3 distances, you would want to know the distance from 1 to 2, and from 2 to 3, and 1 to 3 is irrelevant.我假设,如果你有 3 个距离,你会想知道从 1 到 2 的距离,以及从 2 到 3 的距离,而 1 到 3 是无关紧要的。

For a quick solution, just build yourself a simple struct that looks like that:要获得快速解决方案,只需为自己构建一个简单的结构,如下所示:

struct....
{
   CDestination dest1;
   CDestination dest2;

   float Distance;

   Calculate()
   {
    Distance = /* Your calculation here */
   }  
} 

Not just create a vector of those structs and print the three members.不仅仅是创建这些结构的向量并打印三个成员。 Sorry if thats not what you want, but you didn't provide that much code or information about your problem.....对不起,如果那不是你想要的,但你没有提供那么多关于你的问题的代码或信息......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM