Tag Archives: c++

String operation to delete the character at the specified position

Using iterator and erase

Here, we use a function erase (iterator) of string to delete the characters at the specified position. The iterator iterator can string.begin () get the first iterator of the string, and then add different values to delete it.

	string s = "abcdefg";
	int i = 3;
	string::iterator itr = s.begin();
	itr+=i;
	s.erase(itr);
	s.erase(itr);
	cout<<s;

Here we want to delete “de”. Because we get the first iterator and add the value, we delete “d”. If we want to delete “e”, we don’t need to add any value, because the position of “e” is just “d”, so we just need to erase () again.

main.cpp : (. Text + 0xd06): undefined reference to XX method | simple record

This blog post is just a record of C + + compilation and installation problems – it doesn’t give the correct solution

main.cpp:(.text+0xd06): undefined reference to `uni_text::UniText::UniText(std::string const&, int)'
main.cpp:(.text+0xe73): undefined reference to `uni_text::UniText::PutText(cv::Mat&, std::string const&, cv::Point_<int> const&, cv::Scalar_<double> const&, bool)'

Analysis:
encounter this problem: run some C + + projects that contain special so dynamic library or static library;
the compiled versions of GCC and G + + used by the native environment are not completely consistent with the libraries (dynamic library or static library) compiled by the author, resulting in these errors

1: There is no corresponding dependency (static library or dynamic library). 2: the compiled versions of some static libraries or dynamic libraries in the project are inconsistent

Solutions:

In the final analysis, it’s an environmental issue: GCC, G + + version matching or the supplement of third-party dependency


Conversion from hexadecimal to decimal

Problem Description:
write a program that accepts a hexadecimal value string and outputs the decimal string of the value (note that there may be multiple sets of data in a test case).

Input:
0xa

Output:
10

#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<char> conclude;


int CharToInt(char x){
    if (x >= '0' && x <= '9'){
        return x - '0';
    }else{
        return x - 'A' + 10;
    }
}
void Convert(string str, int x){
    int number = 0;
    for(int i = 0; i < str.size(); ++i){
        number *= x;
        number += CharToInt(str[i]);
    }
    printf("%d\n", number);
}

int main(){
    string str;
    while (cin >> str){
        str = str.substr(2);
        Convert(str, 16);
    }
    return 0;
}

Vs2019: solution nvcc total: cannot find compiler‘ cl.exe ‘ in PATH

vs2019:nvcc fatal : Cannot find compiler ‘ cl.exe ‘ in PATH

Three steps:

    Step 1:
    add the following two positions to the nameless variable path list of system variables
    Step 2:
    create a new variable named Lib, add three locations for it: C: program files (x86), Microsoft Visual Studio, 2019, community, VC, tools, MSVC, 14.27.29110, lib, x64
    C: program files (x86), windows kits, 10, lib, 10.0.18362.0, ucrt, x64
    C: Program files (x86), windows kits, 10, lib, 10.0.18362.0, um, x64 step 3:
    create a new variable named include in the system variable, and add 2 Location: C: program files (x86) – Microsoft Visual Studio (2019) – Community (VC) – tools – MSVC (14.27.29110) – include
    C: program files (x86) – Windows kits (10) – lib (10.0.18362.0) – ucrt

Reference article: http://iliutong.cn/2019/01/20/nvcc-cu-file-in-console-in-windows/

error C4996: ‘inet_addr‘: Use inet_pton() or InetPton() instead or define _WINSOCK_D

[questions]

Vs2015 appears when writing socket client. (this is the second time I have encountered this problem. Last time vs2017, I must remember it to facilitate myself to check in the future: P)

SOCKADDR_ IN ClientAddr;
ClientAddr.sin_ family = AF_ INET;
ClientAddr.sin_ addr.S_ un.S_ addr = inet_ addr(“127.0.0.1”);
ClientAddr.sin_ Port = htons (port);
error c4996: ‘INET_ addr’: Use inet_ pton() or InetPton() instead or define _ WINSOCK_ DEPRECATED_ NO_ WARNINGS to disable deprecated API warnings

[reason]

In the version after vs2013, INET is added_ New functions such as Pton () and inetpton () are used for IP address conversion between “dotted decimal” and “binary integer”, and can handle IPv4 and IPv6. And INET_ Addr is an old function. Higher version vs uses a new function by default when compiling, so this error will be reported.

[solution 1]

Replace INET with the new function suggested_ Addr function.

[solution 2]

Modify the vs configuration and tell it I want the old function

1. Modification method: Item – & gt; attribute – & gt; C / C + + – & gt; general – & gt; SDL check, change “yes” to “no”.
2. Property page of file_ CRT_ SECURE_ NO_ “Warnings” plus
– – –
copyright notice: This is CSDN blogger Rani_ The original article of “ZZ” follows CC 4.0 by-sa copyright agreement. Please attach the link of original source and this statement.
Link to the original text: https://blog.csdn.net/weixin_ 42731241/article/details/96143934

Solution: about the problem “C + + – unresolved inclusion: < iostream >“

Problem description

Develop C + + program with eclipse for C + + and CDT plug-in and compilation tool MinGW, write in the head of C + + source program

#include <iostream>
#include <stdio>

When you wait for such a standard library, the editor will prompt:

Unresolved inclusion: <iostream>

resolvent

First, select the project name, in the selection menu: Project & gt; properties & gt; C / C + + general & gt; preprocessor includes ≫ providers
and check “CDT GCC build in compiler settings”, then click the Apply button.

I’ve tried other solutions on the Internet, but they don’t work.

VC + + compiler can not find the header file and rebuild failure

1. The header file cannot be found

If the associated path of the header file is not set properly, you need to add both the root directory and the subdirectory to the additional header file directory. For example, if there are two header files in the root directory and the subdirectories sub1 and sub2 that need to be referenced elsewhere, you should add the header file path to the additional package directory in this way:

$(SolutionDir)root

$(SolutionDir)root\sub1

$(SolutionDir)root\sub2

2. It is successful to compile a library alone, but rebuild prompts that the related projects fail to compile

In this case, the lazy relationship between project references is not set well. You should right-click on the solution, – gt; common properties – & gt; project dependencies, and set the lazy relationship between project references without omission. In this way, the rebuild will succeed on the top-level project. At the same time, the compilation of dynamic library and other similar problems can also be solved.

 

 

 

The use of C + + template function and lambda expression

#include <iostream>

using namespace std;

class Tmp1 {
public:
    int foo() { 
        cout << "Tmp1.foo" << endl;
        return ret;
    }

    string walk() {
        cout << "walk" << endl;
        return "cout walk";
    }

    int ret = 1;
};

class Tmp2 {
public:
    int foo() {
        cout << "Tmp2.foo" << endl;
        return ret;
    }

    string run() {
        cout << "run" << endl;
        return "cout run";
    }

    int ret = 2;
};

struct OutPara {
    int ret = 0;
    string desc;
};

template<typename T, typename F>
void print(T t, const F &f) {
    cout << "test" << endl;
    f(t);
}

int main()
{
    Tmp1 t1;
    OutPara out;
    print(t1, [&](Tmp1 &t)->void{
        out.desc = t.walk();
        out.ret = t.foo();
    });
    cout << out.ret << "\t" << out.desc << endl;
    Tmp2 t2;
    print(t2, [&](Tmp2 &t)->void{
        out.desc = t.run();
        out.ret = t.foo();
    });
    cout << out.ret << "\t" << out.desc << endl;
    return 0;
}

The output

test
walk
Tmp1.foo
1       cout walk
test
run
Tmp2.foo
2       cout run

Error LNK2019: unresolved external symbol_ Main the symbol is in the function___ Tmaincrtstartup

Error:
Error LNK2019: Unable to resolve external symbol _main, which is referenced in the function ___TMainCrtStartup
Cause analysis,
I use VS2013 debugging an MFC program when the problem occurred.
1. You built a console program with VC. Its entry function should be main, and you used WinMain.

    > You open a.c/.cpp file with vc and compile it directly, using Winmian instead of main as the entry function. VC’s default Settings at this point are for console applications.

Solutions:
1. Enter the project – & gt; setting-> C/C ++, select Preprocessor in Category, remove _CONSOLE in Processor Definitions, and add _WINDOWS
2. Enter the project – & gt; setting-> Link changes /subsystem:console to /subsystem: Windows in Projections.
3. Save Settings, Rebuild All.

Clion automatically adds add_ executable

CLion automatically adds add_executable
Install the C/C++ Single File Execution plugin, search for the C/C++ Single File Execution, find file-settings-plugins, and apply.
The preparations are complete. Next you can create a new C/ CPP file, enter the code, press Ctrl + Alt + Shift +E shortcut or right click in the code area and find Add Executable for Simple C/C++ File, then right click on the item area on the left and select Reload Cmake Project, select Auto-Reload so that it will automatically add to the executable after each right click on Add.

There is a problem of DLL when executing G + + compile command in sublime text editor

Sublime Text sets GCC and g++ (g++ is a superset of GCC, so just g++), most commonly the Mingw folder in dev-cpp.
The problem is as follows:
contains the source file for the input function, which can be compiled in Devc, and a popup window in the sublime text environment shows that the file entry cannot be found and the program input point __gxx_personality_v0 cannot be located in the dynamic link library.

Dynamic Link Library problem caused by DLL files.
Return to the Mingw file where the environment variable was imported and copy the libstdc++6.dll from the lib file.

br>

(Suggestions)
② Paste into the directory where the source file is located.
If there are mistakes, please correct them.