【发布时间】:2014-10-10 05:15:09
【问题描述】:
我现在要做的只是对命令行参数进行排序,但我不断收到分段错误(核心转储)错误,我认为这意味着我有一个指向虚构位置的指针。
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
int main (int argc, char* argv[]) {
vector<int> the_args;
vector<int>::iterator it;
it = the_args.begin(); //this seems logical to me.
int i = 1; //so I'll skip over argv[0]
while (i < argc)
{
the_args.insert (it, atoi(argv[i]));
i++;
it++;//this is probably the perpetrator.
}
sort (the_args.begin(), the_args.end());
for (it = the_args.begin(); it < the_args.end(); it++) //or else it's this
{
cout << *it << " ";
}
return 0;
}
我最终想为游戏编程。我在 Java 方面有足够的经验,我想我可以开始尝试在 C++ 中搞砸并弄清楚......但也许不是?请你的回答很好,我真的很沮丧,我什至不得不在这里问一个关于排序的问题。
【问题讨论】:
-
只使用
push_back,insert无效it。 -
请使用
Boost.Program_optionsboost.org/doc/libs/1_56_0/doc/html/program_options.html 帮自己一个忙。 -
了解一些 Java 并“试图搞砸”将是学习 C++ 的一种缓慢且充满错误的方式。我可以推荐一本好书吗? The Definitive C++ Book Guide and List
标签: c++ sorting vector iterator arguments