2015-01-11 4 views
0

배열의 고유 문자열 수를 계산하는 간단한 프로그램을 만들려고했지만 문자열을 더 반복하면 어떻게해야합니까? 두 번 이상.배열의 고유 문자열 수를 계산합니다.

다음은 "Tommy"가 배열에 3 번 존재하는 코드 예입니다. 따라서 고유하지 않은 수를 세는 경우 3이되어야하며 고유 한 이름은 2 개뿐입니다.

#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    string stringArray[5] = { "Tommy", "Sammy", "Tommy", "Wally", "Tommy" },  repeatedArray[5]; 
    int instance = 0, notUnique = 0, repeated = 0; 

    for (int i = 0; i < 5; i++) 
    { 
     for (int j = i + 1; j < 5; j++) 
     { 
      if (stringArray[i] == stringArray[j] && stringArray[i] != repeatedArray[repeated]) 
      { 
       instance++; 
      } 
     } 

     if (instance == 1) 
     { 
      notUnique += 2; 
     } 

     else if (instance >= 2) 
     { 
      notUnique += instance + 1; 
      repeatedArray[repeated] = stringArray[i]; 
      repeated++; 
     } 

     instance = 0; 
    } 

    cout << "Number of non-unique strings in array is :" << notUnique << endl; 
} 
+0

정렬 배열하고 식별 중복 쉽고 효율적으로 될 것이다. – user3553031

+3

'size_t unique_count = std :: set (stringArray, stringArray + 5) .size();' –

답변

0

편집 : 계산할 내용을 이해하려면 두 번 읽어야합니다. 고맙게도, 내가 염두에 두었던 접근법은 이에 적합합니다.

쉽게 접근 할 수있는 방식으로 문자열을 얼마나 자주 발견했는지 기억하는 것이 쉬운 방법입니다. 내가 std::map 생각 해요 :

#include <iostream> 
#include <string> 
#include <map> 
using namespace std; 

int main() 
{ 
    string stringArray[5] = { "Tommy", "Sammy", "Tommy", "Wally", "Tommy" }; 

    std::map<string, int> count_find; 
    int non_unique_count = 0, unique_count = 0; 

    for(auto &s : stringArray) { 
    // Note: This uses that std::map::operator[] zero-initializes when it is 
    //  called for a new key, so crucially, we start with 0 (not uninitialized) 
    int count = ++count_find[s]; 
    if(count == 1) {   // when a string is found the first time 
     ++unique_count;  // tentatively mark it as unique 
    } else if(count == 2) { // when it is found the second time 
     --unique_count;  // unmark as unique 
     non_unique_count += 2; // and chalk both found strings up as non-unique 
    } else {     // after that, 
     ++non_unique_count; // just chalk up as non-unique. 
    } 
    } 

    std::cout << unique_count << ", " << non_unique_count << std::endl; 
} 

을 다른 방법으로, 루프를 통해 후 당신은 unique_count에서 non_unique_count을 계산할 수

for(auto &s : stringArray) { 
    int count = ++count_find[s]; 
    if(count == 1) {   // when a string is found the first time 
    ++unique_count;  // tentatively mark it as unique 
    } else if(count == 2) { // when it is found the second time 
    --unique_count;  // unmark as unique 
    } 
} 

int non_unique_count = 5 - unique_count; 
0

에서 당신 고유의 수를 계산하려면/비 배열에있는 고유 한 문자열이므로 한 문자열이 고유한지 여부를 알아야합니다.

#include <iostream> #include <string> using namespace std; 
 
int main() { 
 
    string stringArray[5]= { 
 
    "Tommy", "Sammy", "Tommy", "Wally", "Tommy" 
 
    } 
 
    ; 
 
    bool repeate[5];//mark whether the string is unique or not 
 
    for(size_t i=0; 
 
    i<5; 
 
    ++i) repeate[i]=false; 
 
    for (int i=0; 
 
    i < 5; 
 
    i++) { 
 
    for (int j=i + 1; 
 
    j < 5; 
 
    j++) { 
 
     if (stringArray[i]==stringArray[j]) { 
 
     repeate[i]=true; 
 
     repeate[j]=true; 
 
     } 
 
    } 
 
    } 
 
    int notUnique=0; 
 
    for(int i=0; 
 
    i<5; 
 
    ++i) if(repeate[i]) ++notUnique; 
 
    cout <<"Number of non-unique strings in array is :" << notUnique << endl; 
 
}

관련 문제