数组中字符串的不同数目。

时间:2021-09-18 04:26:30

UPDATED WITH FIX

更新修复

I want to return only the number of unique entries in my array. The array contains a string variable that represents IP address. If there are 10 of them, but of 3 different kinds I just want to return 3. I have been at this all day and can't seem to figure out a solution that works. My code:

我只想返回数组中唯一条目的数量。数组包含一个表示IP地址的字符串变量。如果有10个,但有3种,我只想返回3个。我一整天都在做这件事,似乎想不出一个有效的解决办法。我的代码:

Original code

原始代码

int getUnique(Visitors info[], string url, string startDate, string endDate){

int count = 0;

string temp;

for(int i = 0 ; i < N ; i++){

    if(url == info[i].URL && (dateChecker(startDate, endDate, info[i].dateAccessed))){



    }

}

return count;

}

Updated code

更新代码

int getUnique(Visitors info[], string url, string startDate, string endDate){

set<string> ips;

for(int i = 0 ; i < N ; i++){

    if(url == info[i].URL && (dateChecker(startDate, endDate, info[i].dateAccessed))){

       ips.insert(info[i].IP);

    }

}

return ips.size();

}

The first if checks whether the matching URL does in fact match, and the dateChecker just makes sure that the date that the particular IP went to that URL is in between the 2 passed dates (startDate and endDate). How do I get the number of different IPs under these conditions?

第一个if检查匹配的URL是否确实匹配,dateChecker只确保特定IP到达该URL的日期位于两个已通过日期(startDate和endDate)之间。如何在这些条件下得到不同的ip数?

4 个解决方案

#1


2  

Use std::set, it stores items uniquely and it's efficient:

使用std::set,唯一存储物品,高效:

std::set<string> ips;

for (int i=0; i<N; i++)
  ips.insert(info[i].IP);

int unique_ips = ips.size();

#2


1  

You can do this lazily with set

你可以用集合来做这个

std::set<string> uniq;
if(url == info[i].URL && (dateChecker(startDate, endDate, info[i].dateAccessed))){
        uniq.insert(info[i].URL);
}

return uniq.size();

#3


1  

You could use a std::set to keep track of unique strings:

您可以使用std::set来跟踪独特的字符串:

std::set<std::string> mySet ;

for each iteration just do:

对于每一次迭代,只要做:

mySet.insert( info[i].IP) ;

and at the end:

最后:

return mySet.size() ;

#4


1  

As already mentioned you need to use std::set however there is no need to manually iterate array, since std::map accepts iterators to range it should be constructed from. Thus

正如前面提到的,您需要使用std::set,但是不需要手动迭代数组,因为std::map接受迭代器的范围。因此

std::map<std::string> unique(strinvec.begin(), stringvec.end());

Should do the trick.
And if you need to know only count of unique elements, you may use even shorter notation.

应该足够了。如果你只需要知道唯一元素的计数,你可以使用更短的符号。

size_t unique_count = std::map<std::string>(strinvec.begin(), stringvec.end()).size();

#1


2  

Use std::set, it stores items uniquely and it's efficient:

使用std::set,唯一存储物品,高效:

std::set<string> ips;

for (int i=0; i<N; i++)
  ips.insert(info[i].IP);

int unique_ips = ips.size();

#2


1  

You can do this lazily with set

你可以用集合来做这个

std::set<string> uniq;
if(url == info[i].URL && (dateChecker(startDate, endDate, info[i].dateAccessed))){
        uniq.insert(info[i].URL);
}

return uniq.size();

#3


1  

You could use a std::set to keep track of unique strings:

您可以使用std::set来跟踪独特的字符串:

std::set<std::string> mySet ;

for each iteration just do:

对于每一次迭代,只要做:

mySet.insert( info[i].IP) ;

and at the end:

最后:

return mySet.size() ;

#4


1  

As already mentioned you need to use std::set however there is no need to manually iterate array, since std::map accepts iterators to range it should be constructed from. Thus

正如前面提到的,您需要使用std::set,但是不需要手动迭代数组,因为std::map接受迭代器的范围。因此

std::map<std::string> unique(strinvec.begin(), stringvec.end());

Should do the trick.
And if you need to know only count of unique elements, you may use even shorter notation.

应该足够了。如果你只需要知道唯一元素的计数,你可以使用更短的符号。

size_t unique_count = std::map<std::string>(strinvec.begin(), stringvec.end()).size();