Tag Archives: c++

nt service 1072 No need to restart, repeat the method of installing the service program or service driver

Typically, when you install a service program, delete the service, and install again, the system will prompt you with the error code 1072: “The specified service is marked for deletion.” Anti-virus software, for example, needs to be restarted after uninstalling before it can be installed, or it may report an error: “The specified service is marked for deletion.”
Here is a way to repeat the installation without restarting it.
First, close all access instances, that is, close all service handles that are opened by the CreateService and OpenService functions. Close all CreateFile handles.
The ControlService then stops serving.
DeleteService removal service.
As a final step, delete the registry keys “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ Name of your server” and “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\Root\LEGACY_ name of your server”.
 
 
http://zhanyonhu.blog.163.com/blog/static/161860442010223115539853/

To solve the running error of eclipse for C / C + +: launch failed binary not found

sometimes we don’t like to use vs. Dear friends, like to use the eclipse practice c/c + + code, the construction of the environment, portal: http://jingyan.baidu.com/article/17bd8e523d7b9185ab2bb8f2.html

and then you might follow all the above steps and still hang when you run, and then you need to check the following two places:

1. If you haven’t built the project, click on the following position to build it and run it again. If not, scroll down to see

2. Change one setting :



error C2065: ‘cout’ : undeclared identifier

question:

online solution:
1. No

added

#include <iostream>
using namespace std;

or
2. The order of the included libraries needs to be adjusted

//These will not work.
#include <iostream>
using namespace std;
int main () {
    cout << "hey" << endl;
    return 0;
}
#include <iostream>
#include "stdafx.h"
using namespace std;
int main () {
    cout << "hey" << endl;
    return 0;
}
//This will do.

#include "stdafx.h"
#include <iostream>
using namespace std;
int main () {
    cout << "hey" << endl;
    return 0;
}

final solution: my own reasons… Orz)
open iostream file. I commented out part of the code for a project…
just uncomment it.

Error of reading character c + +

this is a very subtle error
this error means that the memory that the variable reads holds a data type that is actually a sequence of characters, not the data type specified by the variable type.

the general reason for this error is:

char* buffer = (char*)malloc(sizeof(char) * 1024);
char *p =  NULL;

what happens when the buffer contains more than 1024 bytes of data?
yes, the value of p is overwritten by buffer. When checking p during debugging, it will be found that p is no longer NULL, and the error will be “error reading character of string”.

C++, we still try to use STL containers, because STL containers will automatically expand. If above code is:

std::string buffer;
char *p =  NULL;

I believe this error will not occur again.

Map to vector pair map.second sort

in leetcode501, there are two little points to learn.

1. Insert element

as an array

map has several insertion methods, please refer to the link for specific differences.

void searchBST(TreeNode* cur, unordered_map<int, int>& map) { // 前序遍历
    if (cur == NULL) return ;
    map[cur->val]++; // 以数组方式插入,统计元素频率
    searchBST(cur->left, map);
    searchBST(cur->right, map);
    return ;
}

. Map into vector sort

needs to be sorted by the occurrence times, that is, by the second element of map, but map is sorted by default for first(key), so I want to convert to vector sorting. Define and initialize the statement as . pair< int, int> > vec(map.begin(), map.end()); , after custom collation sort(vec.begin(), vec.end(), CMP);

the whole code is

class Solution {
private:

void searchBST(TreeNode* cur, unordered_map<int, int>& map) { // 前序遍历
    if (cur == NULL) return ;
    map[cur->val]++; // 统计元素频率
    searchBST(cur->left, map);
    searchBST(cur->right, map);
    return ;
}
bool static cmp (const pair<int, int>& a, const pair<int, int>& b) {
    return a.second > b.second;
}
public:
    vector<int> findMode(TreeNode* root) {
        unordered_map<int, int> map; // key:元素,value:出现频率
        vector<int> result;
        if (root == NULL) return result;
        searchBST(root, map);
        vector<pair<int, int>> vec(map.begin(), map.end());
        sort(vec.begin(), vec.end(), cmp); // 给频率排个序
        result.push_back(vec[0].first);
        for (int i = 1; i < vec.size(); i++) { 
            // 取最高的放到result数组中
            if (vec[i].second == vec[0].second) result.push_back(vec[i].first);
            else break;
        }
        return result;
    }
};

Differences between length() size() and C strlen() of C + + string member functions

1. Function declaration

C++ string member function length() is the same as size(), but strlen() is different from C library function. First take a look at the declaration of the three functions:

//返回string长度,单位字节
size_t length() const noexcept;

//返回string长度,单位字节。作用等同于length()
size_t size() const noexcept;

//C标准库函数,返回C风格字符串长度,单位字节
size_t strlen ( const char * str );

2. Use example

in real projects, when C++ string gets the length, we often use the following two methods.

//方法一:调用length()或size()
string strTest="test";
strTest.length();			//结果为4
strTest.size();				//结果为4

//方法二:转为C风格字符串,调用strlen()
strlen(strTest.c_str());	//结果为4

the above code snippet takes a string length of 4 and makes no difference, so what’s the difference between method 1 and method 2?See the following code:

char buf[256]={0};
buf[0]='a';
buf[2]='v';
buf[3]='h';

string strTest(buf,6);
cout<<"strTest[0]:"<<(uint32_t)strTest[0]<<"_"<<(uint32_t)strTest[1]<<"_"<<(uint32_t)strTest[2]<<"_"<<(uint32_t)strTest[3]<<"_"<<(uint32_t)strTest[4]<<"_"<<(uint32_t)strTest[5]<<endl;
cout<<"strTest.length()="<<strTest.length()<<" strTest.size()="<<strTest.size()<<" strlen(strTest.c_str())="<<strlen(strTest.c_str())<<endl;
cout<<"strTest:"<<strTest<<endl;

output:

strTest[0]:97_0_118_104_0_0
strTest.length()=6 strTest.size()=6 strlen(strTest.c_str())=1
strTest:avh

Conclusion

3.

(1) when a string contains the null character ‘\0’, the length of the string is truncated using strlen(), and the member functions length() and size() are used to return the true length of the string.
(2) cout will filter out null characters when it outputs string, and the output will not be truncated.
(3) when constructing or stitching a string, it is recommended to specify the length of the string at the same time, such as

//构造时使用
string strTest(buf,6);
//而非,因为会被截断
string strTest(buf);

//拼接时使用
strTest.append(buf,6);
//而非,因为会被截断
strTest+=buf;

reference

[1] C++ reference

[leetcode] zigzag transformation

z-sorts a given string from top to bottom, left to right, based on the number of rows given. For example, if the input string is “LEETCODEISHIRING” and the number of lines is 3, list it like this:

L C I R
E T O E S I I G
E D H N

After

, your output needs to be read line by line from left to right to produce a new string, such as “LCIRETOESIIGEDHN”.
:
string convert(string s, int numRows);

  • example 1:
    input: s = “LEETCODEISHIRING”, numRows = 3
    output: “LCIRETOESIIGEDHN”

  • example 2:
    input: s = “LEETCODEISHIRING”, numRows = 4
    0 output: “LDREOEIIECIHNTSG”
    interpretation :
    LDR
    EOEII
    ECIHN
    TSG

Source:

button force (LeetCode)
link: https://leetcode-cn.com/problems/zigzag-conversion


train of thought (have a reference problem solution)

  • the first idea is to establish a two-dimensional array, violence will character in order fill in the
  • set a says going in the direction of variables, when the z glyph to the first line/change direction when the last line
  • dir represents a string array of rows, according to which line going to judge the next character is inserted into the
  • finally connect string array all, become a string

    code

    class Solution {
    public:
        string convert(string s, int numRows) {
            if(numRows==1)
            {
                return s;
            }
            int n=s.length();
            vector<string> rows(min(n,numRows));
            
            int dir=0;
            bool going=false;
            for(char c : s)
            {
                rows[dir]+=c;
                if(dir==0 || dir==numRows-1) going=!going;
                dir+=going?1:-1;
            }
    
            string a;
            for(string x:rows) 
            {
                a+=x;
            }
            return a;
        }
    };
    

    summary

    • get a new method of traversing strings, but also understand the string connection