2010-11-28 9 views
3

저는 Ubuntu의 CodeBlocks IDE에서 g ++을 사용하고 있습니다. STL과 C++의 일부를 처음 사용합니다.STL을 학습하는 동안의 몇 가지 문제

Q1은 : //

std::istream_iterator<std::string> begin (dictionaryFile); 
std::istream_iterator<std::string> end; 
std::vector< std::string> dictionary; 
std::copy (begin, end, std::back_inserter (dictionary)); 

가 올바른 대답,하지만 난

std::istream_iterator<std::string> end(); 

컴파일러는 네 번째 줄에서 일치하는 함수 호출을 말한다에

std::istream_iterator<std::string> end; 

를 변경하는 경우.

Q2 : // 미안 해요 firstLess1와 firstLess2 사이의 유일한 차이는 firstLess1가 PS에 선언되어 있다는 문제가 처음에게

struct PS : std::pair< std::string, std::string > { 
PS(); 
static struct FirstLess: std::binary_function< PS, PS, bool> { 
    bool operator() (const PS & p, const PS & q) const { 
     return p.first < q.first; 
    } 
} firstLess1; }; 


struct FirstLess: std::binary_function< PS, PS, bool> { 
bool operator() (const PS & p, const PS & q) const { 
    return p.first < q.first; 
}} firstLess2; 

주를 취소하지 않았다. 나는 함수를 호출 할 때

는 :

k = std::find_if (j + 1, finis, std::not1 (std::bind1st (PS::firstLess1, *j))); 

컴파일러는 'PS :: firstLess1에 정의되지 않은 참조'나에게 오류를했다. 다음 나는 그

k = std::find_if (j + 1, finis, std::not1 (std::bind1st (firstLess2, *j))); 

로 변경이 컴파일을 통과시켰다.

더 이상, 프로그램의 다른 부분에, 나는 두

j = std::adjacent_find (j , finis, PS::firstLess1); 
j = std::adjacent_find (j , finis, firstLess2); 

을 사용하고 컴파일러는 나에게 오류를 준하지 않았다.

+0

확인. 두 번째 질문을 해결하는 방법을 알아 냈습니다. (하지만 여전히 이유는 모르겠습니다.) 그냥 k = std :: find_if (j + 1, finis, std :: not1 (std :: bind1st (PS :: FirstLess(), * j))); OK 이거나 const PS :: FirstEqual PS :: firstEqual1 = PS :: FirstEqual()을 추가 할 수 있습니다. 선언 후. – user522829

답변

4

std::istream_iterator<std::string> end(); C++은 이것을 end이라는 이름의 함수 선언으로 해석하고 반환 값 유형은 std::istream_iterator<std::string>이고 인수 목록은 비어 있습니다. 그게 왜 그런 실수를하는지. C++에서 클래스의 기본 생성자를 호출하여 객체를 만들려면 type_name variable_name;을 수행하면됩니다. type_name variable_name();은 함수 선언으로 해석됩니다.

+1

이것이 [우리가 가장 애도하는 구문] (http://en.wikipedia.org/wiki/Most_vexing_parse)입니다. –

1

A1 :

그 이니셜 빈} 인 목적, 즉,() 값으로 초기화한다.

[참고 :() 초기화에 대한 구문이 허용되지 않기 때문에

X A();

특정 다른 초기화 문맥 (5.3.4 허용 클래스 X의 객체 선언하지만 함수 선언 X로를 인수를 복용하지 않고 반환

형태(), 5.2 아니다 3, 12.6.2). - 엔드 노트]

A2 :

난 당신이, 표준 : : find_if 및 표준을 :: 원하는 adjacent_find comepletely 뭔가 다른 일을 정말로 확실하지 않다.하지만 어쨌든, 여기에 당신의 영감을받은 작업 코드가 있습니다.

#include <iostream> 
#include <algorithm> 
#include <string> 
#include <vector> 

typedef std::pair<std::string, std::string> TwoStrings; 

struct FirstLess : std::binary_function<TwoStrings, TwoStrings, bool> 
{ 
    bool operator() (const TwoStrings& p, const TwoStrings& q) const { 
     return p.first < q.first; 
    } 
}; 

int main() 
{ 
    std::vector<TwoStrings> v; 
    std::vector<TwoStrings>::iterator it; 
    v.push_back(TwoStrings("4. Here's ", "Johnny")); 
    v.push_back(TwoStrings("1. Here's ", "Johnny")); 
    v.push_back(TwoStrings("2. Here's ", "Johnny")); 
    v.push_back(TwoStrings("2. Here's ", "Johnny")); 
    v.push_back(TwoStrings("3. Here's ", "Johnny")); 

    it = std::adjacent_find(v.begin(), v.end(), FirstLess()); 
    std::cout << it->first << it->second << std::endl; 

    it = std::find_if(v.begin() , v.end(), std::not1(std::bind1st(FirstLess(), *(v.begin())))); 
    std::cout << it->first << it->second << std::endl; 
} 

상기 출력은 다음

1. Here's Johnny 
4. Here's Johnny 
2

A1 : 이미 충분히 답변
A2 : (참조 는 firstLess 선언에 CONST 추가 OR는 CPP 파일의 firstLess 오브젝트를 정의 this)

+0

+1은 * firstLess 객체 *를 정의합니다. 즉, 둘러싼 범위에 있습니다 (링크 된 페이지의 맨 위 줄 참조). 이 경우'const'를 추가하면 구조체가 아닌 정수 유형의 정적 멤버에서만 작동한다는 점에서 차이가 없습니다. (놀라운 것은 컴파일러가'adjacent_find()'호출을 허용한다는 것입니다. –

+0

@j_random_hacker : 그것을 고쳤습니다. 그러나 필수적인 유형의 경우에만 실제로 작동해야합니까? 그것은 과거에 POD 구조체와 함께 저에게도 도움이되었습니다 ... – smerlin

+0

저는 잘 모르겠습니다 :) 나는 그것이 작동 할 것으로 기대해서는 안된다고 말할 수 있습니다. 그러나 컴파일러는 표준에 위배되지 않는 것을 허용 할 자유가 있으며, 나는 그렇게 생각하지 않습니다. –