check if a std::vector contains a certain object?
Checking if v
contains the element x
:
#include <algorithm>
if(std::find(v.begin(), v.end(), x) != v.end()) {
/* v contains x */
} else {
/* v does not contain x */
}
Checking if v
contains elements (is non-empty):
if(!v.empty()){
/* v is non-empty */
} else {
/* v is empty */
}
Test if a vector contains a given element
Both the match()
(returns the first appearance) and %in%
(returns a Boolean) functions are designed for this.
v <- c('a','b','c','e')
'b' %in% v
## returns TRUE
match('b',v)
## returns the first location of 'b', in this case: 2
Check if a vector contains object with already entered values
first of all, you can check count of std::vector to see if given key exists
//std::count(v.begin(), v.end(), key)
if (std::count(v.begin(), v.end(), key)){
//it is inside
}
else{
//it isn't inside
}
This is one way to go, but you should do the map
std::map<std::string,int> myMap;
//adding will look like this
std::pair<std::string,int> element;
myMap.insert(element);
//or
myMap.insert(std::pair<std::string,int>(myString,myInt));
Will solve your problem. You add pair of string and int.
In order to check if given key exists you can do
std::map<std::string, int> m;
if ( m.find(key) == m.end() ) {
// not found
} else {
// found
}
Added Kyle comment to, as it is valid way, and it looks a bit cleaner, you can also find if element exists like this
if (m.count(key)){
//key found
}
else{
//key not found
}
PS. It's bad practice to use c++ using namespace std;
. You should always use std::whatever_you_need
instead
How to find out if an item is present in a std::vector?
You can use std::find
from <algorithm>
:
#include <algorithm>
#include <vector>
vector<int> vec;
//can have other data types instead of int but must same datatype as item
std::find(vec.begin(), vec.end(), item) != vec.end()
This returns an iterator to the first element found. If not present, it returns an iterator to one-past-the-end. With your example:
#include <algorithm>
#include <vector>
if ( std::find(vec.begin(), vec.end(), item) != vec.end() )
do_this();
else
do_that();
R - test if a string vector contains any element of another list
You can get this using grepl
.
lst_A <- c("TET","RNR")
lst_B = c("RNR_B","BC_TET")
Pattern = paste(lst_A, collapse="|")
grepl(Pattern, lst_B)
library(data.table)
DT_result <- data.table(lst_B, result=grepl(Pattern, lst_B))
DT_result
lst_B result
1: RNR_B TRUE
2: BC_TET TRUE
Addition
To respond to a comment, here is an example with more strings to test. Some pass the test, others not.
lst_A <- c("TET","RNR")
lst_B = c("RNR_B","BC_TET", "Fred", "RNR_A", "Zero", "ABC_TET")
Pattern = paste(lst_A, collapse="|")
DT_result <- data.table(lst_B, result=grepl(Pattern, lst_B))
DT_result
lst_B result
1: RNR_B TRUE
2: BC_TET TRUE
3: Fred FALSE
4: RNR_A TRUE
5: Zero FALSE
6: ABC_TET TRUE
How can I determine whether a vector contains another vector respecting order in R?
You can collapse your vector into a regex pattern and use grepl
vec1 <- c("a", "b", "c")
vec2 <- c("a", "b", "c", "d", "e")
grepl(paste(vec1, collapse=".*"), paste(vec2, collapse=""))
# TRUE
vec3 <- c("e", "d", "c", "b", "a")
grepl(paste(vec1, collapse=".*"), paste(vec3, collapse=""))
# FALSE
vec4 <- c("a", "x", "b", "c", "y")
grepl(paste(vec1, collapse=".*"), paste(vec4, collapse=""))
# TRUE
EDIT: Based on G5W's comment, you can add a delimiter in case each element is not a character but might be a short string. The delimiter will break up the entries of your vector
vec5 <- c("a", "b", "c")
vec6 <- c("ab", "c")
vec7 <- c("ab", "e", "c", "d")
grepl(paste(vec5, collapse="-.*"), paste(vec7, collapse="-"))
# FALSE
grepl(paste(vec6, collapse="-.*"), paste(vec7, collapse="-"))
# TRUE
Related Topics
Delete Rows With Negative Values
Remove Ids With Fewer Than 9 Unique Observations
Create and Assign Multiple New Dataframe Columns in Ifelse Statement
Remove Total Value for One Column in Powerbi
Removing Space Between Numeric Values in R
How to Reshape Data from Long to Wide Format
Count Number of Rows Within Each Group
Drop Data Frame Columns by Name
Replace Missing Values (Na) With Most Recent Non-Na by Group
How to Escape Backslashes in R String
Elegant Way to Check For Missing Packages and Install Them
Is There an R Function For Finding the Index of an Element in a Vector
Fixing the Order of Facets in Ggplot
Generating All Distinct Permutations of a List in R
How to Get to the Next Line in the R Command Prompt Without Executing