删除重复的字符串数组c ++

时间:2014-05-11 12:25:06

标签: c++

我想从包含一列字符串的文件中读取,即

AAAA
BBBB
22
4556
.
.
.

并在同一文件中仅重写唯一元素。

    sprintf(nameID,"Try.dat");
    IDFile = fopen (nameID,"r+");
    std::vector<std::string> test; 
    fputs (test,IDFile)
    std::sort(test);
    auto it = std::unique(std::begin(test), std::end(test));
    test.erase(it, test.end());

    for(int k = 0; k<test.size();k++){
        fprintf (IDFile,"%s \n",test[k].c_str());
    }
    fclose (IDFile);

但我收到以下错误

error: cannot convert ‘std::vector<std::basic_string<char> >’ to ‘const char*’ for argument ‘1’ to ‘int fputs(const char*, FILE*)’

error: no matching function for call to ‘sort(std::vector<std::basic_string<char> >&)’

warning: ‘auto’ changes meaning in C++11; please remove it [-Wc++0x-compat]

error: ‘it’ does not name a type

任何帮助/更好的方法吗?

感谢

1 个答案:

答案 0 :(得分:3)

标准库是你的朋友:

#include <set>
#include <iostream>

int main()
{
  std::set<std::string> set;

  for (std::string line; getline(std::cin, line);)
    set.insert(line);

  for (auto const& s : set)
    std::cout << s << '\n';
}