I need some way to know if in my vector n, there is the name that the user writes (although for this example it is "Diana"). I need to know if it is possible to know the position in which it determines that the name attribute of any of the classes of the vector is equal to the name entered.
public:
string name;
Name(string name){
this->name=name;
}
};
int main(){
Name n1("cesar"),n2("diana"),n3("julian");
//vector<int>it={1,2,3,4};
vector<Name> n={n1, n2,n3};
vector<int>::iterator it;
it = find(n.begin(), n.end(), "diana");
if (/* ???? it != end(n) */ ){
cout<<"Encontrado";
}else{
cout<<"no encontrado";
}
system("PAUSE");
return 0;
}
Related
I'm trying to create a TextField in JavaFX (using Scene Builder) that accepts only positive numbers.
I'm trying actually to make a TextField for a 'Credit Card Number' which accepts 13 - 16 numeric values, not including the minus sign at the beginning, and which can accept 0 at the beginning of the text.
Also, since it's a 'Credit Card Number' TextField, I'm looking for a way where I can insert automatic space after each 4 letters/numbers inserted to it.
Note: I know how to create a TextField that accepts only numbers but it also accepts minus sign at the beginning and doesn't accept the 0 as the first input.
Here is what I have been using so my TextField only accepts digits and it works like a charm as below,
public static void digitsTxtFld(TextField field) {
field.setTextFormatter(new TextFormatter<Integer>(change -> {
String newText = change.getControlNewText();
if (newText.matches("\\d*")) {
return change;
}
return null;
}));
}
And to set a character limit I use this,
public static void setTextLimit(TextArea textArea, int length) {
textArea.setTextFormatter(new TextFormatter<>(change -> {
String string = change.getControlNewText();
if (string.length() > length) {
textArea.positionCaret(string.length());
return change;
} else {
return null;
}
}));
}
Edit:
Apparently one cannot have both text formatters as the second one you call will replace the first and your field will only run with one at a time. So I made a joint formatter as follows:
public static void setTextLimitToDigitFld(TextField field, int length) {
field.setTextFormatter(new TextFormatter<Integer>(change -> {
String newText = change.getControlNewText();
if (newText.matches("\\d*") && newText.length() > length) {
return change;
}
return null;
}));
}
I'm trying to use the CList Search method in an application. I have attached a very simple example below.
In this example, I always get a null pointer in the variable result. I tried it in MQL4 and MQL5. Has anyone ever made the Search method work? If so, where is my mistake? With my question, I refer to this implementation of a linked list in MQL (it's the standard implementation). Of course, in my application, I do not want to find the first list item, but items that match specific criteria. But even this trivial example does not work for me.
#property strict
#include <Arrays\List.mqh>
#include <Object.mqh>
class MyType : public CObject {
private:
int val;
public:
MyType(int val);
int GetVal(void);
};
MyType::MyType(int val): val(val) {}
int MyType::GetVal(void) {
return val;
}
void OnStart() {
CList *list = new CList();
list.Add(new MyType(3));
// This returns a valid pointer with
// the correct value
MyType* first = list.GetFirstNode();
// This always returns NULL, even though the list
// contains its first element
MyType* result = list.Search(first);
delete list;
}
CList is a kind of linked list. A classical arraylist is CArrayObj in MQL4/5 with Search() and some other methods. You have to sort the list (so implement virtual int Compare(const CObject *node,const int mode=0) const method) before calling search.
virtual int MyType::Compare(const CObject *node,const int mode=0) const {
MyType *another=(MyType*)node;
return this.val-another.GetVal();
}
void OnStart(){
CArrayObj list=new CArrayObj();
list.Add(new MyType(3));
list.Add(new MyType(4));
list.Sort();
MyType *obj3=new MyType(3), *obj2=new MyType(2);
int index3=list.Search(obj3);//found, 0
int index2=list.Search(obj2);//not found, -1
delete(obj3);
delete(obj2);
}
I'm trying to write values to some OPC UA nodes with Qt and open62541. For that I have to know the different data types of the nodes. Every time I try to read the data type of a node, I get a Boolean type instead of an int32. It is the correct node in the list and I can read all nodes. Can someone please help me?
//This is how I add nodes to the server. This is an example with the node DBW0.
//After adding the nodes to the server, each node will be append to the _nodeList.
void OPCConnection::AddNodeToServer()
{
QOpcUaNodeCreationAttributes attributes;
attributes.setDataTypeId(QOpcUa::namespace0Id(QOpcUa::NodeIds::Namespace0::Int16));
attributes.setValueRank(-2); // Scalar or array
attributes.setAccessLevel(QOpcUa::AccessLevelBit::CurrentWrite);
attributes.setUserAccessLevel(QOpcUa::AccessLevelBit::CurrentWrite);
QOpcUaAddNodeItem item;
item.setParentNodeId(QOpcUa::QExpandedNodeId("ns=2;s=PLC1.S7_300.DB120"));
item.setReferenceTypeId(QOpcUa::nodeIdFromReferenceType(QOpcUa::ReferenceTypeId::Organizes));
item.setRequestedNewNodeId(QOpcUa::QExpandedNodeId("ns=2;s=DBW0"));
item.setNodeClass(QOpcUa::NodeClass::Variable);
item.setNodeAttributes(attributes);
_client->addNode(item);
}
//This is how I read the nodes.
void OPCConnection::readNode()
{
if (_client->state() == QOpcUaClient::ClientState::Connected)
{
for (int i = 0; i < _nodeList->count(); i++)
{
_nodeList->at(i)->readAttributes(QOpcUa::NodeAttribute::DataType);
_nodeList->at(i)->readAttributes(QOpcUa::NodeAttribute::Value);
}
}
}
//After reading I want to write.
void OPCConnection::setNodeValue(const QVariant value, const int index)
{
_nodeList->at(index)->writeValueAttribute(value,
_nodeList->at(index)->attribute(QOpcUa::NodeAttribute::DataType).
value<QOpcUa::Types>());
}
I can only write boolean nodes as a datatype, because each node has a boolean value as a datatype.
I found a solution for my case, but I'm not happy with that.
Because I can't distinguish a 16 bit Integer and a 32 bit Integer.
void OPCConnection::setNodeValue(const QVariant value, const int index)
{
_nodeList->at(index)->writeValueAttribute(value,
selectType(_nodeList->at(index)->attribute(QOpcUa::NodeAttribute::Value).type()));
}
QOpcUa::Types OPCConnection::selectType(const QVariant::Type type)
{
switch (type)
{
case QVariant::Bool:
return QOpcUa::Types::Boolean;
case QVariant::UInt:
return QOpcUa::Types::UInt32;
default:
return QOpcUa::Types::UInt16;
}
}
Here is what I've done so far:
struct rep_list {
struct node *head;
struct node *tail;
}
typedef rep_list *list;
int length(const list lst) {
if (lst->head == NULL) {
return 0;
}
else {
lst->head = lst->head->next;
return 1 + length(lst);
}
}
This works, but the head of the list the function accepts as a parameter gets changed. I don't know how to fix that.
I'm not allowed to change the function definition so it should always accept a list variable.
Any ideas?
EDIT: I tried to do what Tyler S suggested in the comments but I encountered another problem. If I create a node* variable at the beginning, it should point to lst->head. But then every recursive call to the function changes the value back to lst->head and I cannot move forward.
You don't need a local node: just don't change the list head. Instead, pass the next pointer as the recursion head.
int length(const list lst) {
if (lst->head == NULL) {
return 0;
}
else {
return 1 + length(lst->head-next);
}
}
I see. Okay; this gets a bit clunky because of the chosen representation. You need a temporary variable to contain the remaining list. This iscludes changing the head.
int length(const list lst) {
if (lst->head == NULL) {
return 0;
}
else {
new_lst = new(list)
new_lst->head = lst->head->next;
var result = 1 + length(new_lst);
free(new_lst)
return result
}
}
At each recursion step, you create a new list object, point it to the 2nd element of the current list, and continue. Does this do the job for you?
Although this solution is clunky and I hate it, its the only way I can see to accomplish what you're asking without modifying the method signature. We create a temporary node * as member data of the class and modify it when we start.
struct rep_list {
struct node *head;
struct node *tail;
}
node *temp = NULL;
bool didSetHead = false;
typedef rep_list *list;
int length(const list lst) {
if ((didSetHead) && (lst->head != temp)) {
temp = lst->head;
didSetHead = false;
}
if (temp == NULL) {
didSetHead = true;
return 0;
}
else {
temp = temp->next;
return 1 + length(temp);
}
}
Please note, I haven't tested this code and you may have to play with a bit, but the idea will work.
I have declared my c# application constant this way:
public class Constant
public struct profession
{
public const string STUDENT = "Student";
public const string WORKING_PROFESSIONAL = "Working Professional";
public const string OTHERS = "Others";
}
public struct gender
{
public const string MALE = "M";
public const string FEMALE = "F";
}
}
My validation function:
public static bool isWithinAllowedSelection(string strValueToCheck, object constantClass)
{
//convert object to struct
//loop thru all const in struct
//if strValueToCheck matches any of the value in struct, return true
//end of loop
//return false
}
During runtime, I will like pass in the user inputted value and the struct to check if the value exist in the struct. The struct can be profession and gender. How can I achieve it?
Example:
if(!isWithinAllowedSelection(strInput,Constant.profession)){
response.write("invalid profession");
}
if(!isWithinAllowedSelection(strInput,Constant.gender)){
response.write("invalid gender");
}
You probably want to use enums, not structs with constants.
Enums gives you a lot of possibilities, it is not so hard to use its string values to save it to the database etc.
public enum Profession
{
Student,
WorkingProfessional,
Others
}
And now:
To check existence of value in Profession by value's name:
var result = Enum.IsDefined(typeof(Profession), "Retired"));
// result == false
To get value of an enum as a string:
var result = Enum.GetName(typeof(Profession), Profession.Student));
// result == "Student"
If you really can't avoid using value names with whitespaces or other special characters, you can use DescriptionAttribute:
public enum Profession
{
Student,
[Description("Working Professional")] WorkingProfessional,
[Description("All others...")] Others
}
And now, to get description from Profession value you can use this code (implemented here as an extension method):
public static string Description(this Enum e)
{
var members = e.GetType().GetMember(e.ToString());
if (members != null && members.Length != 0)
{
var attrs = members.First()
.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length != 0)
return ((DescriptionAttribute) attrs.First()).Description;
}
return e.ToString();
}
This method fetches description defined in attribute and if there's none, returns value name. Usage:
var e = Profession.WorkingProfessional;
var result = e.Description();
// result == "Working Professional";