在C ++中查找并删除字符串向量中的字符串

时间:2012-04-08 02:36:36

标签: c++

我正在尝试在字符串向量中找到一个2部分的名称,我有我认为可以工作但它没有。我不能使用迭代器。这是我的代码,它是更大程序的一部分,它是具有5个函数的程序中的单个函数。

int deletenames()
{
    ifstream inFile;
    ofstream outFile;
    string strFileName;
    string strFName,strLName;
    vector<string> vecStudent;
    string strDFName, strDLName;
    int i=0;
    char line[80];
    //delets a name
    cout << endl<< " Enter a name to be deleted( First and Last Name):";
    cin>>strDFName>>strDLName;

    for(i<0;strDFName+ " "+strDLName=strFName+ " "+strLName; i++)
    {
        if(strDFName+ " "+strDLName=strFName+ " "+strLName)
            {
                vecStudent.erase;
                cout << "Student Deleted";
        }
    }

    // open output file for writing
    outFile.open(strFileName.c_str());
    if ( outFile.fail())
    {
        cout<<" Output file error! Student was not added"<<endl;
        return -1;
    }

    //display the content of the vector
    for(int i=0; i<vecStudent.size(); i++)
        cout<< vecStudent[i]<<endl;

    for(int i=0; i<vecStudent.size();i++)
        outFile<<vecStudent[i]<<endl;
    outFile.close();
return 0;
}

我从Microsoft Visual Studio收到以下错误消息我正在使用它来编译它。

main.cpp
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(78): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(64): warning C4101: 'line' : unreferenced local variable
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(105): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(108): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(90): warning C4101: 'line' : unreferenced local variable
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(132): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(135): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(122): warning C4101: 'line' : unreferenced local variable
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(158): warning C4552: '<' : operator has no effect; expected operator with side-effect
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(158): error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(160): error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(162): error C3867: 'std::vector<_Ty>::erase': function call missing argument list; use '&std::vector<_Ty>::erase' to create a pointer to member
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(179): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\ace\desktop\cpsc 1103 assingment 4\cpsc 1103 assingment 4\main.cpp(182): warning C4018: '<' : signed/unsigned mismatch

1 个答案:

答案 0 :(得分:0)

似乎可以肯定的是,(x; a=b; a++)if (a=b)所拥有的地方中至少有一个(可能是两个)实际上是为了比较而不是分配(即,您打算使用{ {1}}代替==)。

特别是,在这两种情况下,左侧是通过将一些字符串连接在一起而构建的临时值。一般来说,分配到临时是不可能的。即使有可能,通常也是一个错误。

顺便说一句,您似乎也复制了代码以显示数组的内容(即,您有两个代码副本)。此外,除非这是一个你必须“手工”做事的作业,你可能想要使用=而不是写一个循环来找到字符串。